Nth multiple of a number in Fibonacci Series in Python
Last Updated :
28 Mar, 2025
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 number, we stop and return it. For example, if n = 2 and m = 3, we need to find the second Fibonacci number that is divisible by 3. Looking at the sequence, the numbers that are multiples of 3 are 3, 21, 144, 987, .... The second such number is 21.
Using iterative approach
The iterative approach generates Fibonacci numbers sequentially and checks for divisibility by m. The method maintains a count of Fibonacci numbers that are divisible by m, stopping when the nth multiple is found. Since it only computes Fibonacci numbers as needed, it avoids unnecessary computations, making it optimal for large values.
Python
n, m = 4, 3
a, b, count = 0, 1, 0
while True:
a, b = b, a + b # Compute next Fibonacci number
if b % m == 0: # Check if divisible by m
count += 1
if count == n: # If nth multiple is found, print it
print(b)
break
Explanation:
- We initialize a = 0 and b = 1 as the first two Fibonacci numbers.
- The loop generates Fibonacci numbers using a, b = b, a + b.
- It checks if b is divisible by m. If yes, it increments count.
- When count == n, we print b and stop the loop.
Using dynamic programming appproch
The dynamic programming method optimizes Fibonacci sequence computation by storing previously computed values in a list. This avoids redundant calculations and reduces the time complexity significantly compared to the recursive approach. The idea is to maintain a list of Fibonacci numbers and iterate through it to find the nth occurrence of a multiple of m.
Python
def fun(n, m):
fib = [0, 1]
count = 0 # Track multiples of m
while True:
fib.append(fib[-1] + fib[-2]) # Generate next Fibonacci
if fib[-1] % m == 0:
count += 1
if count == n:
return fib[-1]
n, m = 4, 3 # 4th Fibonacci multiple of 3
print(fun(n,m))
Explanation: This approach initializes a list fib with the first two Fibonacci numbers. It then enters a loop, continuously appending the sum of the last two elements to generate the Fibonacci sequence. Each new Fibonacci number is checked for divisibility by m. If a multiple is found, the counter increments. When the counter reaches n, the function returns the required Fibonacci number.
Recursive approach with memorization
Recursive approach calculates Fibonacci numbers using a recursive function, enhanced with memorization to store already computed values. While this approach is conceptually straightforward, it is less efficient than iterative and dynamic programming methods because recursive calls introduce function call overhead.
Python
def fib(n, memo={0: 0, 1: 1}):
if n not in memo:
memo[n] = fib(n - 1, memo) + fib(n - 2, memo) # Compute & store
return memo[n]
def fun(n, m):
count, idx = 0, 2 # Counter & index starting from the third Fibonacci number
while True:
value = fib(idx) # Compute Fibonacci number
if value % m == 0:
count += 1
if count == n:
return value
idx += 1 # Move to the next index
n, m = 4, 3 # 4th Fibonacci multiple of 3
print(fun(n, m))
Explanation: recursive fib(n) function to compute Fibonacci numbers, storing results in a dictionary (memo) to avoid redundant calculations. The main function initializes a counter and an index (idx = 2, starting from the third Fibonacci number). It iterates through Fibonacci numbers, checking for divisibility by m. If a multiple is found, the counter increments. When the counter reaches n, the function returns the Fibonacci number.
Similar Reads
How to Check if a Given Number is Fibonacci number - Python Fibonacci numbers are part of a famous sequence where each number is the sum of the two preceding ones, i.e. F(n) = F(n-1) + F(n-2). The sequence starts as:0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...Notice that every number is equal to the sum of its previous 2 numbers. In this article, we will learn how t
2 min read
Find the position of number that is multiple of certain number In this type of question, a number is given and we have to find the position or index of all multiples of that number. For doing this question we use a function named numpy.argwhere(). Syntax: numpy.argwhere(array) Example 1:Â Sometimes we need to find the indexes of the element that are divisible b
1 min read
Python | sympy.fibonacci() method With the help of sympy.fibonacci() method, we can find the Fibonacci number and Fibonacci polynomial in SymPy. fibonacci(n) - The Fibonacci numbers are the integer sequence defined by the initial terms F_0 = 0 , F_1 = 1 and the two-term recurrence relation F_n = F_{n-1} + F_{n-2} . Syntax: fibonacci
2 min read
NumPy - Fibonacci Series using Binet Formula All of us are familiar with Fibonacci Series. Each number in the sequence is the sum of the two numbers that precede it. So, the sequence goes: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34...... In this tutorial, we will implement the same using NumPy with the aid of Binet formula. Binet Formula fn = \left [\lef
3 min read
Print first m multiples of n without using any loop in Python 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 Comprehensio
3 min read