- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find the GCD of N Fibonacci Numbers with given Indices in C++
Here we have to find the GCD of n Fibonacci terms with the given indices. So at first we have to get the maximum index, and generate Fibonacci terms, some Fibonacci terms are like this: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ….. The index is starts from 0. So the element at 0th index is 0. If we have to find gcd of Fibonacci terms at indices {2, 3, 4, 5}, then the terms are {1, 2, 3, 4}, so GCD of these numbers are 1.
We will use one interesting approach to do this task. To get the GCD of ith and jth Fibonacci term like GCD(Fibo(i), Fibo(j)), we can express it like Fibo(GCD(i, j))
Example
#include <iostream> #include <algorithm> using namespace std; int getFiboTerm(int n){ int fibo[n + 2]; fibo[0] = 0; fibo[1] = 1; for(int i = 2; i<= n; i++){ fibo[i] = fibo[i - 1] + fibo[i - 2]; } return fibo[n]; } int getNFiboGCD(int arr[], int n){ int gcd = 0; for(int i = 0; i < n; i++){ gcd = __gcd(gcd, arr[i]); } return getFiboTerm(gcd); } int main() { int indices[] = {3, 6, 9}; int n = sizeof(indices)/sizeof(indices[0]); cout << "GCD of fibo terms using indices: " << getNFiboGCD(indices, n); }
Output
GCD of fibo terms using indices: 2
- Related Articles
- Maximum GCD of N integers with given product in C++
- C++ Program to Find the GCD and LCM of n Numbers
- Find two numbers whose sum and GCD are given in C++
- Count pairs of natural numbers with GCD equal to given number in C++
- Find any pair with given GCD and LCM in C++
- Find GCD of two numbers
- Count Fibonacci numbers in given range in O(Log n) time and O(1) space in C++
- Program to find GCD of floating point numbers in C++
- Find GCD of factorial of elements of given array in C++
- Find original numbers from gcd() every pair in C++
- Program to find GCD or HCF of two numbers in C++
- C program to find GCD of numbers using recursive function
- Find out the GCD of two numbers using while loop in C language
- Program to find minimum number of Fibonacci numbers to add up to n in Python?
- Find permutation of first N natural numbers that satisfies the given condition in C++

Advertisements