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.
Similar Reads
Create multiple copies of a string in Python by using multiplication operator In Python, creating multiple copies of a string can be done in a simple way using multiplication operator. In this article, we will focus on how to achieve this efficiently.Using multiplication operator (*)This method is the easiest way to repeat a string multiple times in Python. It directly gives
1 min read
Multiply All Numbers in the List in Python Our task is Multiplying all numbers in a list Using Python. This can be useful in calculations, data analysis, and whenever we need a cumulative product. In this article we are going to explore various method to do this. Using a loopWe can simply use a loop (for loop) to iterate over the list elemen
2 min read
Nth multiple of a number in Fibonacci Series in Python Our task is to find the nth multiple of a number in the Fibonacci series which involves identifying Fibonacci numbers that are exactly divisible by a given number m. This means we will check each Fibonacci number one by one and select only those that are multiples of m. Once we find the nth such num
4 min read
Python Program for Number of elements with odd factors in given range Given a range [n,m], find the number of elements that have odd number of factors in the given range (n and m inclusive). Examples: Input : n = 5, m = 100 Output : 8 The numbers with odd factors are 9, 16, 25, 36, 49, 64, 81 and 100 Input : n = 8, m = 65 Output : 6 Input : n = 10, m = 23500 Output :
2 min read
Specifying the increment in for-loops in Python Let us see how to control the increment in for-loops in Python. We can do this by using the range() function. range() function range() allows the user to generate a series of numbers within a given range. Depending on how many arguments the user is passing to the function, the user can decide where
2 min read