📌  相关文章
📜  检查第n个斐波那契数是否为10的倍数的有效方法

📅  最后修改于: 2021-04-23 18:16:36             🧑  作者: Mango

给我们一个变量n,我们需要确定斐波那契数是否是10的倍数。
例子:

Input : 15
Output : Yes

Input : 17
Output : No

一个简单的方法是找到第n个斐波那契数,并检查它是否可以被10整除。

C++
// A simple C++ program to check if
// n-th Fibonacci number is multiple
// of 10.
#include
 
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


Javascript


C++
// A simple C++ program to check if
// n-th Fibonacci number is multiple
// of 10.
#include
 
// 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


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
"); else     document.write("No
");       // This code is contributed by _saurabh_jaiswal


输出:

Yes

高效方法:
如果n非常大,上述解决方案可能无法正常工作,那么就不可能找到斐波那契数。此外,通过查看模式,我们可以在没有找到斐波那契数的情况下进行检查。让我们看看如何!
如果数字可以被10整除,则必须必须被5和2整除。
斐波那契数列2的倍数:
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 987 1597 2584 …。
粗体显示的数字可被2整除。仔细观察,我们发现每个第三个数字都可被2整除。
斐波那契数列中5的倍数:
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 987 1597 2584……
粗体显示的数字可被5整除。仔细观察,我们发现每5个数字都可被5整除。
现在3和5的LCM为15。因此,每15个斐波那契数将被10整除。因此,我们不需要查找斐波那契数,只需要检查n是否可被15整除即可。下面是实现。

C++

// A simple C++ program to check if
// n-th Fibonacci number is multiple
// of 10.
#include
 
// 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


Java脚本

// 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
"); else     document.write("No
");       // This code is contributed by _saurabh_jaiswal

输出:

Yes