Friday, December 27, 2024
Google search engine
HomeData Modelling & AIAn efficient way to check whether n-th Fibonacci number is multiple of...

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

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

Examples:

Input : 15
Output : Yes

Input : 17
Output : No

A Simple Method is to find the 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 code is contributed by ajit.
?>


Javascript




<script>
 
// A simple Javascript program to
// check if n-th Fibonacci
// number is multiple of 10.
function fibonacci(n)
{
    let a = 0;
    let b = 1;
    let c;
    if (n <= 1)
        return n;
    for (let 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)
{
    let f = fibonacci(30);
    return (f % 10 == 0);
}
 
// Driver code
    let n = 30;    
    if (isMultipleOf10(n))
        document.write("Yes<br>");
    else
        document.write("No\n");
 
// This code is contributed by _saurabh_jaiswal.
</script>


Output

Yes

Time complexity: O(n) 
Auxiliary space: O(1)

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
?>


Javascript




// A simple Javascript 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
let n = 30;
if (isMultipleOf10(n))
    document.write("Yes<br>");
else
    document.write("No<br>");
     
// This code is contributed by _saurabh_jaiswal


Output

Yes

Time Complexity: O(1) time. 
Auxiliary space: O(1) 

This article is contributed by Aditya Kumar. If you like neveropen and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the neveropen 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.
 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments