📜  斐波那契数列中数字的n个倍数

📅  最后修改于: 2021-04-23 22:07:17             🧑  作者: Mango

给定两个整数n和k。找到斐波那契数列中K的第n个倍数的位置。
例子 :

Input : k = 2, n = 3
Output : 9
3'rd multiple of 2 in Fibonacci Series is 34 
which appears at position 9.

Input  : k = 4, n = 5 
Output : 30
4'th multiple of 5 in Fibonacci Series is 832040 
which appears at position 30.

斐波那契数列(F):1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,10946,17711, 28657、46368、75025、121393、196418、317811、514229、832040…(忽略第一个0)。
一个简单的解决方案是遍历从第一个数字开始的斐波那契数字。遍历时,请跟踪k的倍数计数。每当计数变为n时,就返回位置。
一个有效的解决方案基于以下有趣的属性。
斐波那契数列在模块表示下始终是周期性的。以下是示例。

F (mod 2) = 1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,
            1,1,0,1,1,0,1,1,0,1,1,0,1,1,0 
Here 0 is repeating at every 3rd index and 
the cycle repeats at every 3rd index. 

F (mod 3) = 1,1,2,0,2,2,1,0,1,1,2,0,2,2,1,0
            ,1,1,2,0,2,2,1,0,1,1,2,0,2,2
Here 0 is repeating at every 4th index and 
the cycle repeats at every 8th index.

F (mod 4) = 1,1,2,3,1,0,1,1,2,3,1,0,1,1,2,3,
           1,0,1,1,2,3,1,0,1,1,2,3,1,0 
Here 0 is repeating at every 6th index and 
the cycle repeats at every 6th index.

F (mod 5) = 1,1,2,3,0,3,3,1,4,0,4,4,3,2,0,
            2,2,4,1,0,1,1,2,3,0,3,3,1,4,0
Here 0 is repeating at every 5th index and
the cycle repeats at every 20th index.

F (mod 6) = 1,1,2,3,5,2,1,3,4,1,5,0,5,5,4,
            3,1,4,5,3,2,5,1,0,1,1,2,3,5,2
Here 0 is repeating at every 12th index and 
the cycle repeats at every 24th index.

F (mod 7) = 1,1,2,3,5,1,6,0,6,6,5,4,2,6,1,
            0,1,1,2,3,5,1,6,0,6,6,5,4,2,6 
Here 0 is repeating at every 8th index and 
the cycle repeats at every 16th index.

F (mod 8) = 1,1,2,3,5,0,5,5,2,7,1,0,1,1,2,
            3,5,0,5,5,2,7,1,0,1,1,2,3,5,0 
Here 0 is repeating at every 6th index and 
the cycle repeats at every 12th index.

F (mod 9) = 1,1,2,3,5,8,4,3,7,1,8,0,8,8,7,
            6,4,1,5,6,2,8,1,0,1,1,2,3,5,8 
Here 0 is repeating at every 12th index and 
the cycle repeats at every 24th index.

F (mod 10) = 1,1,2,3,5,8,3,1,4,5,9,4,3,7,0,
             7,7,4,1,5,6,1,7,8,5,3,8,1,9,0.
Here 0 is repeating at every 15th index and
the cycle repeats at every 60th index.

为什么斐波那契数列在模数下是周期性的?
在模数表示下,我们知道每个斐波那契数将表示为一些残差0? F(mod m)资料来源:https://www.whitman.edu/Documents/Academics/Mathematics/clancy.pdf
基于上述事实,我们可以通过简单地找到第一个倍数来快速找到K的第n个倍数的位置。如果第一个倍数的位置是i,我们将位置返回为n * i。
下面是实现:

C++
// C++ program to find position
// of n'th multiple of a number
// k in Fibonacci Series
# include 
using namespace std;
 
const int MAX = 1000;
 
// Returns position of n'th multple
// of k in Fibonacci Series
int findPosition(int k, int n)
{
    // Iterate through all
    // fibonacci numbers
    unsigned long long int f1 = 0,
                           f2 = 1,
                           f3;
    for (int i = 2; i <= MAX; i++)
    {
        f3 = f1 + f2;
        f1 = f2;
        f2 = f3;
 
        // Found first multiple of
        // k at position i
        if (f2 % k == 0)
 
        // n'th multiple would be at
        // position n*i using Periodic
        // property of Fibonacci numbers
        // under modulo.
        return n * i;
    }
}
 
// Driver Code
int main ()
{
    int n = 5, k = 4;
    cout << "Position of n'th multiple of k"
        <<" in Fibonacci Series is "
        << findPosition(k, n) << endl;
    return 0;
}


Java
// Java Program to find position
// of n'th multiple of a number
// k in Fibonacci Series
 
class GFG
{
    public static int findPosition(int k,
                                   int n)
    {
        long f1 = 0, f2 = 1, f3;
        int i = 2;
 
        while(i != 0)
        {
            f3 = f1 + f2;
            f1 = f2;
            f2 = f3;
 
            if(f2 % k == 0)
            {
                return n * i;
            }
 
            i++;
        }
        return 0;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // Multiple no.
        int n = 5;
 
        // Number of whose multiple
        // we are finding
        int k = 4;
 
        System.out.print("Position of n'th multiple" +
                     " of k in Fibonacci Series is ");
 
        System.out.println(findPosition(k, n));
    }
}
 
// This code is contributed
// by Mohit Gupta_OMG


Python3
# Python Program to find position
# of n'th multiple of a number k
# in Fibonacci Series
 
def findPosition(k, n):
    f1 = 0
    f2 = 1
    i = 2;
    while i != 0:
        f3 = f1 + f2;
        f1 = f2;
        f2 = f3;
 
        if f2 % k == 0:
            return n * i
 
        i += 1
         
    return
 
 
# Multiple no.
n = 5;
# Number of whose multiple
# we are finding
k = 4;
 
print("Position of n'th multiple of k in"
      "Fibonacci Seires is", findPosition(k, n));
 
# This code is contributed
# by Mohit Gupta_OMG


C#
// C# Program to find position of
// n'th multiple of a mumber k in
// Fibonacci Series
using System;
 
class GFG
{
    static int findPosition(int k, int n)
    {
        long f1 = 0, f2 = 1, f3;
        int i = 2;
 
        while(i!=0)
        {
            f3 = f1 + f2;
            f1 = f2;
            f2 = f3;
 
            if(f2 % k == 0)
            {
              return n * i;
            }
 
            i++;
        }
        return 0;
    }
     
    // Driver code
    public static void Main()
    {
        // Multiple no.
        int n = 5;
 
        // Number of whose multiple
        // we are finding
        int k = 4;
 
        Console.Write("Position of n'th multiple " + 
                      "of k in Fibonacci Series is ");
         
        // Function calling
        Console.WriteLine(findPosition(k, n));
    }
}
 
// This code is contributed by Sam007


PHP


Javascript


输出 :

Position of n'th multiple of k in Fibonacci Series is 30