C/C++ Program for nth multiple of a number in Fibonacci Series
Last Updated :
28 May, 2022
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++
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1000;
int findPosition( int k, int n)
{
unsigned long long int f1 = 0, f2 = 1, f3;
for ( int i = 2; i <= MAX; i++) {
f3 = f1 + f2;
f1 = f2;
f2 = f3;
if (f2 % k == 0)
return n * i;
}
}
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
C Program for n-th Fibonacci number
In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation Fn = Fn-1 + Fn-2with seed values F0 = 0 and F1 = 1.Method 1 ( Use recursion ) C/C++ Code //Fibonacci Series using Recursion #include<stdio.h> int fib(int n) { if (n <= 1) return n; return fib(n
4 min read
C Program to Print Fibonacci Series
The Fibonacci series is the sequence where each number is the sum of the previous two numbers of the sequence. The first two numbers are 0 and 1 which are used to generate the whole series. Example Input: n = 5Output: 0 1 1 2 3Explanation: The first 5 terms of the Fibonacci series are 0, 1, 1, 2, 3.
4 min read
C/C++ Program to Count trailing zeroes in factorial of a number
Given an integer n, write a function that returns count of trailing zeroes in n!. Examples : Input: n = 5 Output: 1 Factorial of 5 is 120 which has one trailing 0. Input: n = 20 Output: 4 Factorial of 20 is 2432902008176640000 which has 4 trailing zeroes. Input: n = 100 Output: 24Trailing 0s in n! =
2 min read
C/C++ Program for Number of solutions to Modular Equations
Given A and B, the task is to find the number of possible values that X can take such that the given modular equation (A mod X) = B holds good. Here, X is also called a solution of the modular equation. Examples: Input : A = 26, B = 2 Output : 6 Explanation X can be equal to any of {3, 4, 6, 8, 12,
5 min read
C Program to Find All Factors of Number
In this article, we will learn to write a C program to print all distinct divisors of a number n. Factors of a number are the positive integers that divide the number without leaving a remainder. For example, for n = 10, the factors will be 1, 2, 5 and 10. Note: This problem is different from findin
2 min read
C Program to Find minimum sum of factors of number
Write a C program for a given number N, the task is to find the minimum sum of its factors. Examples: Input : 12Output : 7Explanation: Following are different ways to factorize 12 andsum of factors in different ways.12 = 12 * 1 = 12 + 1 = 1312 = 2 * 6 = 2 + 6 = 812 = 3 * 4 = 3 + 4 = 712 = 2 * 2 * 3
2 min read
C Program for Find sum of odd factors of a number
Write a C program for a given number n, the task is to find the odd factor sum. Examples: Input : n = 30Output : 24Explanation: Odd dividers sum 1 + 3 + 5 + 15 = 24 Input : 18Output : 13Explanation: Odd dividers sum 1 + 3 + 9 = 13 C Program for Find sum of odd factors of a number using Prime Factori
3 min read
C/C++ Program to Find remainder of array multiplication divided by n
Write a C/C++ program for a given multiple numbers and a number n, the task is to print the remainder after multiplying all the numbers divided by n. Examples: Input: arr[] = {100, 10, 5, 25, 35, 14}, n = 11Output: 9Explanation: 100 x 10 x 5 x 25 x 35 x 14 = 61250000 % 11 = 9Input : arr[] = {100, 10
3 min read
C/C++ Program to Count number of binary strings without consecutive 1's
Write a C/C++ program for a given positive integer N, the task is to count all possible distinct binary strings of length N such that there are no consecutive 1s. Examples: Input: N = 2Output: 3// The 3 strings are 00, 01, 10 Input: N = 3Output: 5// The 5 strings are 000, 001, 010, 100, 101 Recommen
4 min read
C/C++ Program for nth Catalan Number
Catalan numbers are defined as a mathematical sequence that consists of positive integers, which can be used to find the number of possibilities of various combinations. The nth term in the sequence denoted Cn, is found in the following formula: [Tex]\frac{(2n)!}{((n + 1)! n!)} [/Tex] The first few
6 min read