Skip to content
Related Articles

Related Articles

An efficient way to check whether n-th Fibonacci number is multiple of 10
  • Difficulty Level : Medium
  • Last Updated : 27 Dec, 2018

We are given a variable n, we need to find whether Fibonacci number will be multiple of 10 or not.

Examples:

Input : 15
Output : Yes

Input : 17
Output : No

A Simple Method is to find nth Fibonacci number and check if it is divisible by 10 or not.

C++




// A simple C++ program to check if
// n-th Fibonacci number is multiple
// of 10.
#include<bits/stdc++.h>
  
int fibonacci(int n)
{
    int a = 0, b = 1, c;
    if (n <= 1)
        return n;
    for (int i = 2; i<= n; i++)
    {
        c = a + b;
        a = b;
        b = c;
    }
    return c;
}
  
// Returns true if n-th Fibonacci number 
// is multiple of 10.
bool isMultipleOf10(int n)
{
    int f = fibonacci(30);
    return  (f % 10 == 0);
}
  
// Driver code
int main()
{
    int n = 30;
    if (isMultipleOf10(n))
        printf("Yes\n");
    else
        printf("No\n");
}


Java




// A simple Java program to check if
// n-th Fibonacci number is multiple
// of 10.
class Fibonacci
{
    static int fibonacci(int n)
    {
        int a = 0;
        int b=1;
        int c=0;
        if (n <= 1)
            return n;
          
        for (int i = 2; i<= n; i++)
        {
            c = a + b;
            a = b;
            b = c;
        }
          
        return c;
    }
      
    // Returns true if n-th Fibonacci number 
    // is multiple of 10.
    static boolean isMultipleOf10(int n)
    {
        int f = fibonacci(30);
        return  (f % 10 == 0);
    }
      
    // main function
    public static void main (String[] args) 
    {
        int n = 30;
        if (isMultipleOf10(n))
            System.out.println("Yes");
        else
            System.out.println("No");
    }


Python 3




# A simple Python 3 program to check if
# n-th Fibonacci number is multiple
# of 10.
  
def fibonacci(n):
  
    a = 0
    b = 1
    if (n <= 1):
        return n
    for i in range(2, n + 1):
      
        c = a + b
        a = b
        b = c
      
    return c
  
# Returns true if n-th Fibonacci 
# number is multiple of 10.
def isMultipleOf10(n):
    f = fibonacci(30)
    return (f % 10 == 0)
  
# Driver code
if __name__ =="__main__":
      
    n = 30
    if (isMultipleOf10(n)):
        print("Yes")
    else:
        print("No")
  
# This code is contributed by ita_c


C#




// A simple C# program to check if
// n-th Fibonacci number is multiple
// of 10.
using System;
  
class GFG {
      
    static int fibonacci(int n)
    {
        int a = 0;
        int b = 1;
        int c = 0;
        if (n <= 1)
            return n;
          
        for (int i = 2; i<= n; i++)
        {
            c = a + b;
            a = b;
            b = c;
        }
          
        return c;
    }
      
    // Returns true if n-th Fibonacci
    // number is multiple of 10.
    static bool isMultipleOf10(int n)
    {
        int f = fibonacci(30);
        return (f % 10 == 0);
    }
      
    // main function
    public static void Main () 
    {
        int n = 30;
          
        if (isMultipleOf10(n))
            Console.Write("Yes");
        else
            Console.Write("No");
    }
}
  
// This code contribute by parshar.


PHP




<?php
  
// A simple PHP program to
// check if n-th Fibonacci
// number is multiple of 10.
  
function fibonacci($n)
{
    $a = 0; $b = 1; $c;
    if ($n <= 1)
        return $n;
    for ($i = 2; $i<= $n; $i++)
    {
        $c = $a + $b;
        $a = $b;
        $b = $c;
    }
    return $c;
}
  
// Returns true if n-th Fibonacci 
// number is multiple of 10.
function isMultipleOf10($n)
{
    $f = fibonacci(30);
    return ($f % 10 == 0);
}
  
// Driver code
    $n = 30;
      
    if (isMultipleOf10($n))
        echo "Yes\n";
    else
        echo "No\n";
  
// This ocde is contributed by ajit.
?>



Output:



Yes

Efficient Method :

The above solution may not work if n is very large, then it is not possible to find fibonacci number. Moreover, we can check without finding fibonacci number by looking on the pattern. Let’s see how !

If number is divisible by 10, then it must have to be divisible by 5 and 2 both.

Multiples of 2 in Fibonacci Series :
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 ….
The number shown in bold are divisible by 2. On careful observation, we finds that every 3rd number is divisible by 2.

Multiples of 5 in Fibonacci Series :
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 ……

The number shown in bold are divisible by 5. On careful observation, we find that every 5-th number is divisible by 5.

Now LCM of 3 and 5 is 15. So, every 15-th Fibonacci number will be divisible by 10. So, we don’t need to find Fibonacci number, just we have to check if n is divisible by 15 or not. Below is the implementation.

C++




// A simple C++ program to check if
// n-th Fibonacci number is multiple
// of 10.
#include<bits/stdc++.h>
  
// Returns true if n-th Fibonacci number 
// is multiple of 10.
bool isMultipleOf10(int n)
{
    return  (n % 15 == 0);
}
  
int main()
{
    int n = 30;
    if (isMultipleOf10(n))
        printf("Yes\n");
    else
        printf("No\n");
    return 0;
}


Java




// A simple Java program to check if
// n-th Fibonacci number is multiple
// of 10.
class Fibonacci
{
    // Returns true if n-th Fibonacci number 
    // is multiple of 10.
    static boolean isMultipleOf10(int n)
    {
        if(n%15 == 0)
            return  true;
          
        return false;    
    }
      
    // main function
    public static void main (String[] args) 
    {
        int n = 30;
        if (isMultipleOf10(n))
            System.out.println("Yes");
        else
            System.out.println("No");
    }


Python3




# A simple Python 3 program to check if
# n-th Fibonacci number is multiple
# of 10.
  
# Returns true if n-th Fibonacci number 
# is multiple of 10.
def isMultipleOf10(n):
  
    return (n % 15 == 0)
  
# Driver Code
n = 30
  
if (isMultipleOf10(n)):
    print("Yes");
else:
    print("No");
  
# This code is contributed
# by Akanksha Rai


C#




// A simple C# program to check if
// n-th Fibonacci number is multiple
// of 10.
using System;
  
class GFG {
  
    // Returns true if n-th Fibonacci number 
    // is multiple of 10.
    static bool isMultipleOf10(int n)
    {
        if(n % 15 == 0)
            return  true;
           
        return false;    
    }
       
    // main function
    public static void Main () 
    {
        int n = 30;
        if (isMultipleOf10(n))
            Console.Write("Yes");
        else
            Console.Write("No");
    }
  
// This code is contributed by nitin mittal.


PHP




<?php
// A simple PHP program to 
// check if n-th Fibonacci 
// number is multiple of 10.
  
// Returns true if n-th 
// Fibonacci number is
// multiple of 10.
function isMultipleOf10($n)
{
    return ($n % 15 == 0);
}
  
// Driver Code
$n = 30;
if (isMultipleOf10($n))
    echo "Yes\n";
else
    echo "No\n";
      
// This code is contributed 
// by akt_mit
?>



Output:

Yes

This code runs in O(1) time.

This article is contributed by Aditya Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :