Print first m multiples of n without using any loop in Python
Last Updated :
19 Dec, 2024
While loops are the traditional way to print first m multiples of n, Python provides other efficient methods to perform this task without explicitly using loops. This article will explore different approaches to Printing first m multiples of n without using any loop in Python.
Using List Comprehension
List comprehension provides a compact way to generate lists. Here, it can be used to calculate the multiples of n by iterating over a range of numbers.
Python
n = 5
m = 10
# Loop through numbers from 1 to 'm'
#(inclusive) and multiply each by 'n'
multiples = [n * i for i in range(1, m + 1)]
print(multiples)
Output[5, 10, 15, 20, 25, 30, 35, 40, 45, 50]
Explanation:
range(1, m + 1) generates numbers from 1 to mmm, inclusive.- For each number in the range, it computes n×i.
- The result is stored in a list
multiples.
Let's explore some more methods to print first m multiples of n without using any loop in Python.
Using map()
map() function applies a given function to each item of an iterable. It can be used to compute multiples of n for the required range.
Python
n = 5
m = 10
# Use the map with lambda calculate the multiples of 'n'
# range function generates
# numbers from 1 to 'm' (inclusive)
multiples = list(map(lambda x: n * x, range(1, m + 1)))
print(multiples)
Output[5, 10, 15, 20, 25, 30, 35, 40, 45, 50]
Explanation:
range(1, m + 1) generates numbers from 1 to m.- The
lambda function multiplies each number in the range by n. map() applies the lambda function to each element and the results are converted to a list using list().
Using NumPy
NumPy library is a powerful tool for numerical operations in Python. Its array manipulation capabilities make it an efficient choice for generating multiples.
Python
import numpy as np
n = 5
m = 10
# Use NumPy's arange function to create an array of multiples
# Start at 'n', end at 'n * m + 1' (exclusive)
multiples = list(np.arange(n, n * m + 1, n))
print(multiples)
Output[5, 10, 15, 20, 25, 30, 35, 40, 45, 50]
Explanation:
np.arange(start, stop, step) generates values from n to n×m with a step of n.- The result is converted into a list using
list().
Using * (Unpacking Operator)
unpacking operator can be used creatively to print the multiples without explicitly storing them in a list.
Python
n = 5
m = 10
#Use a list comprehension to
#generate the multiples of 'n'
#'*' unpacks the list, printing
#its elements separated by spaces
print(*[n * i for i in range(1, m + 1)])
Output5 10 15 20 25 30 35 40 45 50
Explanation:
[n * i for i in range(1, m + 1)] generates a list of multiples.* operator unpacks the list and prints its elements separated by spaces.
Using Recursion
Recursion is another way to compute and print multiples without using loops. It involves repeatedly calling a function until a base condition is met.
Python
def print_multiples(n, m, current=1):
if current > m:
return
print(n * current, end=' ')
print_multiples(n, m, current + 1)
n = 5
m = 10
print_multiples(n, m)
Output5 10 15 20 25 30 35 40 45 50
Explanation:
- The function
print_multiples takes three arguments: the number n, the total multiples m and the current multiplier (defaulted to 1). - Base Condition: Stops when
current > m. - Recursive Call: Prints n×current and calls itself with
current + 1.
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice