Skip to content
Related Articles

Related Articles

Check if a M-th fibonacci number divides N-th fibonacci number
  • Difficulty Level : Easy
  • Last Updated : 17 Jul, 2018

Given two numbers M and N, the task is to check if the M-th and N-th Fibonacci numbers perfectly divide each other or not.

Examples:

Input: M = 3, N = 6
Output: Yes
F(3) = 2, F(6) = 8 and F(6) % F(3) = 0

Input: M = 2, N = 9
Output: No

A naive approach will be to find the N-th and M-th Fibonacci numbers and check if they are perfectly divisible or not.



An efficient approach is to use the Fibonacci property to determine the result. If m perfectly divides n, then Fm also perfectly divides Fn, else it does not.

Exception: When N is 2, it is always possible as Fibo2 is 1, which divides every other Fibonacci number.

Below is the implementation of the above approach:

C++




// C++ program to check if 
// M-th fibonacci divides N-th fibonacci
#include <bits/stdc++.h>
using namespace std;
  
void check(int n, int m)
{
    // exceptional case for F(2)
    if (n == 2 || m == 2 || n % m == 0) {
        cout << "Yes" << endl;
    }
    // if none of the above cases,
    // hence not divisible
    else {
        cout << "No" << endl;
    }
}
  
// Driver Code
int main()
{
    int m = 3, n = 9;
    check(n, m);
  
    return 0;
}


Java




// Java program to check
// if M-th fibonacci 
// divides N-th fibonacci
import java.io.*;
  
class GFG 
{
static void check(int n, int m)
{
    // exceptional case for F(2)
    if (n == 2 || m == 2 ||
        n % m == 0
    {
        System.out.println( "Yes");
    }
      
    // if none of the above cases,
    // hence not divisible
    else 
    {
        System.out.println( "No");
    }
}
  
// Driver Code
public static void main (String[] args) 
{
    int m = 3, n = 9;
    check(n, m);
}
}
  
// This code is contributed
// by anuj_67.


Python 3




# Python 3 program to 
# check if M-th fibonacci 
# divides N-th fibonacci
  
def check(n, m):
  
    # exceptional case for F(2)
    if (n == 2 or m == 2 or
        n % m == 0) :
        print( "Yes" )
      
    # if none of the above 
    # cases, hence not divisible
    else :
        print( "No" )
  
# Driver Code
m = 3
n = 9
check(n, m)
  
# This code is contributed
# by Smitha


C#




// C# program to check
// if M-th fibonacci 
// divides N-th fibonacci
using System;
  
class GFG 
{
static void check(int n, int m)
{
    // exceptional case for F(2)
    if (n == 2 || m == 2 ||
        n % m == 0) 
    {
         Console.WriteLine( "Yes");
    }
      
    // if none of the above cases,
    // hence not divisible
    else
    {
        Console.WriteLine( "No");
    }
}
  
// Driver Code
public static void Main () 
{
    int m = 3, n = 9;
    check(n, m);
}
}
  
// This code is contributed
// by anuj_67.


PHP




<?php
// PHP program to check 
// if M-th fibonacci 
// divides N-th fibonacci
  
function check($n, $m)
{
    // exceptional case for F(2)
    if ($n == 2 || $m == 2 || 
        $n % $m == 0) 
    {
        echo "Yes" , "\n";
    }
      
    // if none of the above cases,
    // hence not divisible
    else 
    {
        echo "No" ," \n";
    }
}
  
// Driver Code
$m = 3; $n = 9;
check($n, $m);
  
// This code is contributed
// by anuj_67.
?>


Output:

Yes

Time Complexity: O(1).

competitive-programming-img

My Personal Notes arrow_drop_up
Recommended Articles
Page :