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
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem