

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 Questions & Answers
- C++ Program to Find the GCD and LCM of n Numbers
- Find GCD of two numbers
- Maximum GCD of N integers with given product in C++
- Find all good indices in the given Array in Python\n
- Find two numbers whose sum and GCD are given in C++
- Count pairs of natural numbers with GCD equal to given number in C++
- Java Program to Find GCD of two Numbers
- Find indices with None values in given list in Python
- Find any pair with given GCD and LCM in C++
- Program to find minimum number of Fibonacci numbers to add up to n in Python?
- Java program to find the GCD or HCF of two numbers
- Find GCD of factorial of elements of given array in C++
- Program to find GCD of floating point numbers in C++
- Python – Find the sum of Length of Strings at given indices
- Find permutation of first N natural numbers that satisfies the given condition in C++
Advertisements