An efficient way to check whether n-th Fibonacci number is multiple of 10?

CServer Side ProgrammingProgramming

Here we will see one efficient way to check whether the nth Fibonacci term is multiple of 10 or not. Suppose the Fibonacci terms are {0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987}. So here 15th (Counting from 0) Fibonacci term is divisible by 10. For 16 it will return true.

One easiest method is generating Fibonacci numbers up to given term, and check whether it is divisible by 10 or not? But this solution is not good, because it will not work for larger terms.

Another good approach is like below −

Fibonacci terms − 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987

These numbers (marked as bold letters) are divisible by 2. And their intervals are 3 Fibonacci terms. Similarly, please check that −

Fibonacci terms: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987

Every 5th term is divisible by 5. Now LCM of 3 and 5 is 15. So we can say that every 15th Fibonacci terms are divisible by 10.

Let us see the algorithm to get the idea.

Algorithm

fiboDivTen(term)

Begin
   if term is divisible by 15, then
      return true
   end if
   return false
End

Example

#include<iostream>
using namespace std;
bool fiboDivTen(int term) {
   if(term % 15 == 0){
      return true;
   }
   return false;
}
int main() {
   int term = 45;
   if (fiboDivTen(term))
      cout << "Divisible";
   else
      cout << "Not Divisible";
}

Output

Divisible
raja
Published on 31-Jul-2019 16:50:58
Advertisements