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 appears at position 30.
Fibonacci Series(F) : 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040… (neglecting the first 0).
A Simple Solution is to traverse Fibonacci numbers starting from first number. While traversing, keep track of counts of multiples of k. Whenever the count becomes n, return the position.
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.
Why is Fibonacci Series Periodic under Modulo?
Under modular representation, we know that each Fibonacci number will be represented as some residue 0 ? F (mod m) < m. Thus, there are only m possible values for any given F (mod m) and hence m*m = m^2 possible pairs of consecutive terms within the sequence. Since m^2 is finite, we know that some pair of terms must eventually repeat itself. Also, as any pair of terms in the Fibonacci sequence determines the rest of the sequence, we see that the Fibonacci series modulo m must repeat itself at some point, and thus must be periodic.
Source : https://www.whitman.edu/Documents/Academics/Mathematics/clancy.pdf
Based on above fact, we can quickly find position of n'th multiple of K by simply finding first multiple. If position of first multiple is i, we return position as n*i.
Below is the implementation :
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;
}
Java
// Java Program to find position
// of n'th multiple of a number
// k in Fibonacci Series
class GFG
{
public static int findPosition(int k,
int n)
{
long f1 = 0, f2 = 1, f3;
int i = 2;
while(i != 0)
{
f3 = f1 + f2;
f1 = f2;
f2 = f3;
if(f2 % k == 0)
{
return n * i;
}
i++;
}
return 0;
}
// Driver Code
public static void main(String[] args)
{
// Multiple no.
int n = 5;
// Number of whose multiple
// we are finding
int k = 4;
System.out.print("Position of n'th multiple" +
" of k in Fibonacci Series is ");
System.out.println(findPosition(k, n));
}
}
// This code is contributed
// by Mohit Gupta_OMG
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));
# This code is contributed
# by Mohit Gupta_OMG
C#
// C# Program to find position of
// n'th multiple of a number k in
// Fibonacci Series
using System;
class GFG
{
static int findPosition(int k, int n)
{
long f1 = 0, f2 = 1, f3;
int i = 2;
while(i!=0)
{
f3 = f1 + f2;
f1 = f2;
f2 = f3;
if(f2 % k == 0)
{
return n * i;
}
i++;
}
return 0;
}
// Driver code
public static void Main()
{
// Multiple no.
int n = 5;
// Number of whose multiple
// we are finding
int k = 4;
Console.Write("Position of n'th multiple " +
"of k in Fibonacci Series is ");
// Function calling
Console.WriteLine(findPosition(k, n));
}
}
// This code is contributed by Sam007
PHP
<?php
// PHP program to find position
// of n'th multiple of a number
// k in Fibonacci Series
$MAX = 1000;
// Returns position of n'th multiple
// of k in Fibonacci Series
function findPosition($k, $n)
{
global $MAX;
// Iterate through all
// fibonacci numbers
$f1 = 0; $f2 = 1; $f3;
for ($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
$n = 5; $k = 4;
echo("Position of n'th multiple of k" .
" in Fibonacci Series is " .
findPosition($k, $n));
// This code is contributed by Ajit.
?>
JavaScript
<script>
// Javascript program to find position
// of n'th multiple of a number
// k in Fibonacci Series
let MAX = 1000;
// Returns position of n'th multiple
// of k in Fibonacci Series
function findPosition(k, n)
{
// Iterate through all
// fibonacci numbers
let f1 = 0;
let f2 = 1;
let f3;
for (let 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
let n = 5;
let k = 4;
document.write("Position of n'th multiple of k" +
" in Fibonacci Series is " +
findPosition(k, n));
// This code is contributed by _saurabh_jaiswal
</script>
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.
Similar Reads
C/C++ Program for nth 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 5'th multiple of 4 in Fibonacci Series is 832040 which appear
4 min read
Find the sum of first N odd Fibonacci numbers Given a number, N. Find the sum of first N odd Fibonacci numbers. Note: The answer can be very large so print the answer modulo 10^9+7. Examples: Input : N = 3Output : 5Explanation : 1 + 1 + 3Input : 6Output : 44Explanation : 1 + 1 + 3 + 5 + 13 + 21Approach: Odd Fibonacci series is: 1, 1, 3, 5, 13,
9 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
Sum of numbers in the Kth level of a Fibonacci triangle Given a number K, the task is to find the sum of numbers at the Kth level of the Fibonacci triangle.Examples: Input: K = 3 Output: 10 Explanation: Fibonacci triangle till level 3: 0 1 1 2 3 5 Sum at 3rd level = 2 + 3 + 5 = 10 Input: K = 2 Output: 2 Explanation: Fibonacci triangle till level 3: 0 1 1
6 min read
Sum of numbers in the Kth level of a Fibonacci triangle Given a number K, the task is to find the sum of numbers at the Kth level of the Fibonacci triangle.Examples: Input: K = 3 Output: 10 Explanation: Fibonacci triangle till level 3: 0 1 1 2 3 5 Sum at 3rd level = 2 + 3 + 5 = 10 Input: K = 2 Output: 2 Explanation: Fibonacci triangle till level 3: 0 1 1
6 min read
Sum of numbers from 1 to N which are in Fibonacci Sequence Given an integer N, the task is to find the sum of numbers from 1 to N which are in Fibonacci sequence. First few Fibonacci numbers are 1, 1, 2, 3, 5, 8, 13, 21, 34, ....Examples: Input: N = 5 Output: 12 1 + 1 + 2 + 3 + 5 = 12Input: N = 10 Output: 20 1 + 1 + 2 + 3 + 5 + 8 = 20 Approach: Loop through
4 min read