Given two integers n and k. Find position the n'th multiple of K in the Fibonacci series.
Examples:
Input : k = 2, n = 3
Output : 9
3'rd multiple of 2 in Fibonacci Series is 34
which appears at position 9.
Input : k = 4, n = 5
Output : 30
5'th multiple of 4 in Fibonacci Series is 832040
which appears at position 30.
An Efficient Solution is based on below interesting property.
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.
F (mod 8) = 1, 1, 2, 3, 5, 0, 5, 5, 2, 7, 1, 0, 1, 1, 2,
3, 5, 0, 5, 5, 2, 7, 1, 0, 1, 1, 2, 3, 5, 0
Here 0 is repeating at every 6th index and
the cycle repeats at every 12th index.
F (mod 9) = 1, 1, 2, 3, 5, 8, 4, 3, 7, 1, 8, 0, 8, 8, 7,
6, 4, 1, 5, 6, 2, 8, 1, 0, 1, 1, 2, 3, 5, 8
Here 0 is repeating at every 12th index and
the cycle repeats at every 24th index.
F (mod 10) = 1, 1, 2, 3, 5, 8, 3, 1, 4, 5, 9, 4, 3, 7, 0,
7, 7, 4, 1, 5, 6, 1, 7, 8, 5, 3, 8, 1, 9, 0.
Here 0 is repeating at every 15th index and
the cycle repeats at every 60th index.
C++
// C++ program to find position of n'th multiple
// of a number k in Fibonacci Series
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1000;
// Returns position of n'th multiple of k in
// Fibonacci Series
int findPosition(int k, int n)
{
// Iterate through all fibonacci numbers
unsigned long long int f1 = 0, f2 = 1, f3;
for (int i = 2; i <= MAX; i++) {
f3 = f1 + f2;
f1 = f2;
f2 = f3;
// Found first multiple of k at position i
if (f2 % k == 0)
// n'th multiple would be at position n*i
// using Periodic property of Fibonacci
// numbers under modulo.
return n * i;
}
}
// Driver Code
int main()
{
int n = 5, k = 4;
cout << "Position of n'th multiple of k"
<< " in Fibonacci Series is "
<< findPosition(k, n) << endl;
return 0;
}
Output: Position of n'th multiple of k in Fibonacci Series is 30
Time Complexity: O(1000), the code will run in a constant time.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Please refer complete article on n'th multiple of a number in Fibonacci Series for more details!
Similar Reads
n'th multiple of a number in Fibonacci Series Given two integers n and k. Find position the n'th multiple of K in the Fibonacci series. Examples : Input : k = 2, n = 3 Output : 9 3'rd multiple of 2 in Fibonacci Series is 34 which appears at position 9. Input : k = 4, n = 5 Output : 30 4'th multiple of 5 in Fibonacci Series is 832040 which appea
7 min read
Program to find last two digits of Nth Fibonacci number Given a number ânâ, write a function that prints the last two digits of n-th (ânâ can also be a large number) Fibonacci number.Examples: Input : n = 65 Output : 65 Input : n = 365 Output : 65 Recommended: Please solve it on âPRACTICEâ first, before moving on to the solution. A simple solution is to
9 min read
Program to find Nth odd Fibonacci Number Given an integer N. The task is to find the Nth odd Fibonacci number.The odd number fibonacci series is as: 1, 1, 3, 5, 13, 21, 55, 89, 233, 377, 987, 1597.............and so on.Note: In the above series we have omitted even terms from the general fibonacci sequence. Examples: Input: N = 3 Output: 3
3 min read
C++ Program for How to check if a given number is Fibonacci number? Given a number 'n', how to check if n is a Fibonacci number. First few Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 141, .. Examples : Input : 8 Output : Yes Input : 34 Output : Yes Input : 41 Output : No Following is an interesting property about Fibonacci numbers that can also be
2 min read
Program to find last digit of n'th Fibonacci Number Given a number 'n', write a function that prints the last digit of n'th ('n' can also be a large number) Fibonacci number. Examples : Input : n = 0 Output : 0 Input: n = 2 Output : 1 Input : n = 7 Output : 3 Recommended PracticeThe Nth FibonnaciTry It! Method 1 : (Naive Method) Simple approach is to
13 min read
An efficient way to check whether n-th Fibonacci number is multiple of 10 We are given a variable n, we need to find whether Fibonacci number will be a multiple of 10 or not. Examples: Input : 15Output : Yes Input : 17Output : No A Simple Method is to find the nth Fibonacci number and check if it is divisible by 10 or not. C++ // A simple C++ program to check if // n-th F
7 min read