Open In App

Nth multiple of a number in Fibonacci Series in Python

Last Updated : 28 Mar, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

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.

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

Output
987

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.

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))

Output
987

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.

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))

Output
987

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.



Next Article
Article Tags :

Similar Reads

three90RightbarBannerImg