Open In App
Related Articles

Python Program for nth multiple of a number in Fibonacci Series

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Given two integers n and k. Find position the nth multiple of K in the Fibonacci series. 
Examples:

Input: k = 2, n = 3
Output: 9, 3rd multiple of 2 in Fibonacci Series is 34 that appears at position 9.

Input: k = 4, n = 5 
Output: 30, 5th multiple of 5 in Fibonacci Series is 832040 which appears at position 30.

An Efficient Solution is based on the below interesting property. 
The Fibonacci series is always periodic under modular representation. Below are examples. 

F (mod 2) = {{1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0}, {1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0}}
Here 0 is repeating at every 3rd index and the cycle repeats at every 3rd index. 

F (mod 3) = {{1, 1, 2, 0, 2, 2, 1, 0, 1, 1, 2, 0, 2, 2, 1, 0}, {1, 1, 2, 0, 2, 2, 1, 0, 1, 1, 2, 0, 2, 2}}
Here 0 is repeating at every 4th index and the cycle repeats at every 8th index.

F (mod 4) = {{1,1,2,3,1,0,1,1,2,3,1,0,1,1,2,3},{1,0,1,1,2,3,1,0,1,1,2,3,1,0}}
Here 0 is repeating at every 6th index and the cycle repeats at every 6th index.

F (mod 5) = {{1,1,2,3,0,3,3,1,4,0,4,4,3,2,0}, {2,2,4,1,0,1,1,2,3,0,3,3,1,4,0}}
Here 0 is repeating at every 5th index and the cycle repeats at every 20th index.

F (mod 6) = {{1,1,2,3,5,2,1,3,4,1,5,0,5,5,4}, {3,1,4,5,3,2,5,1,0,1,1,2,3,5,2}}
Here 0 is repeating at every 12th index and the cycle repeats at every 24th index.

F (mod 7) = {{1,1,2,3,5,1,6,0,6,6,5,4,2,6,1}, {0,1,1,2,3,5,1,6,0,6,6,5,4,2,6 }}
Here 0 is repeating at every 8th index and the cycle repeats at every 16th index.

Below is the implementation of above approach:

Python3

# Python Program to find position of n\'th multiple
# of a number k in Fibonacci Series
 
def findPosition(k, n):
    f1 = 0
    f2 = 1
    i =2;
    while i!=0:
        f3 = f1 + f2;
        f1 = f2;
        f2 = f3;
 
        if f2%k == 0:
            return n*i
 
        i+=1
         
    return
 
 
# Multiple no.
n = 5;
# Number of whose multiple we are finding
k = 4;
 
print("Position of n\'th multiple of k in"
                "Fibonacci Series is", findPosition(k,n));
 
# Code contributed by Mohit Gupta_OMG

                    

Output
Position of n'th multiple of k inFibonacci Series is 30

Time Complexity: O(N), where N is the index of nth multiple of k
Auxiliary Space: O(1)

Method: Iterative approach using a list to store Fibonacci series

  1. First, we generate the Fibonacci series till the nth term.
  2. Then, we check if the current term is a multiple of k or not.
  3. If it is a multiple of k, we increment our counter variable.
  4. Once the counter becomes equal to n, we return the position of the current term in the Fibonacci series.

Steps:

  1. Define a function nth_fib_multiple that takes two integers n and k as input.
  2. Initialize a list called fibonacci with the first two terms of the Fibonacci series.
  3. Initialize a variable called counter with value 0.
  4. Iterate from the third term of the Fibonacci series till the end.
  5. Check if the current term is a multiple of k or not.
  6. If it is a multiple of k, increment the counter.
  7. Check if the counter is equal to n or not.
  8. If it is equal to n, return the position of the current term in the Fibonacci series.
  9. If the counter is not equal to n, append the current term to the fibonacci list and continue the loop.

Python3

def nth_fib_multiple(n, k):
    fibonacci = [0, 1]
    counter = 0
    for i in range(2, 10000):
        current = fibonacci[i-1] + fibonacci[i-2]
        if current % k == 0:
            counter += 1
            if counter == n:
                return i
        fibonacci.append(current)
         
# Example usage
print(nth_fib_multiple(3, 2)) # Output: 9

                    

Output
9

The time complexity of this function is O(n), where n is the nth multiple of k we are looking for. 
The auxiliary space used by this function is O(n).

Approach: Recursive Fibonacci series with memoization.

Steps:

  1. Define a recursive function fib(n) that returns the nth number in the Fibonacci series.
  2. Implement memoization to store the values of previously calculated Fibonacci numbers.
  3. Define a function fib_multiple(k, n) that finds the nth multiple of k in the Fibonacci series.
  4. Iterate through the Fibonacci series until the nth multiple of k is found.
  5. Return the position of the nth multiple of k in the Fibonacci series.

Python3

# Recursive function to find the nth number in the Fibonacci series
def fib(n, memo):
    if n == 1 or n == 2:
        return 1
    if memo[n] != 0:
        return memo[n]
    memo[n] = fib(n-1, memo) + fib(n-2, memo)
    return memo[n]
 
# Function to find the nth multiple of k in the Fibonacci series
def fib_multiple(k, n):
    memo = [0] * 100
    count = 0
    for i in range(1, 100):
        if fib(i, memo) % k == 0:
            count += 1
            if count == n:
                return i
    return -1
 
# Test the function with sample inputs
k = 2
n = 3
result = fib_multiple(k, n)
print(f"The {n}th multiple of {k} in the Fibonacci series is at position {result}")
 
k = 4
n = 5
result = fib_multiple(k, n)
print(f"The {n}th multiple of {k} in the Fibonacci series is at position {result}")

                    

Output
The 3th multiple of 2 in the Fibonacci series is at position 9
The 5th multiple of 4 in the Fibonacci series is at position 30

Please refer complete article on n’th multiple of a number in Fibonacci Series for more details!



Last Updated : 27 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads