斐波那契数列定义为 = + 在哪里 = 1并且 = 1是种子。
对于给定的质数p,请考虑一个新序列,即(斐波那契序列)mod p。例如,对于p = 5,新序列将为1、1、2、3、0、3、3、1、4、0、4、4…
新序列的最小零定义为第一个斐波那契数,它是p或p的倍数。 mod p = 0。
给定素数p,找到序列Fibonacci模p的最小零。
例子:
Input : 5
Output : 5
The fifth Fibonacci no (1 1 2 3 5)
is divisible by 5 so 5 % 5 = 0.
Input : 7
Output : 8
The 8th Fibonacci no (1 1 2 3 5 8 13 21)
is divisible by 7 so 21 % 7 = 0.
一种简单的方法是继续计算斐波那契数,并为每个数计算Fi mod p。但是,如果我们观察到这个新序列,则让表示序列的项,则如下: =( + )mod pie其余部分实际上是本系列前两个术语余数的总和。因此,我们无需生成斐波那契数列,然后对每个项取模,我们只需将前两个余数相加,然后取其模数p。
下面是查找最小值0的实现。
C++
// C++ program to find minimal 0 Fibonacci
// for a prime number p
#include
using namespace std;
// Returns position of first Fibonacci number
// whose modulo p is 0.
int findMinZero(int p)
{
int first = 1, second = 1, number = 2, next = 1;
while (next)
{
next = (first + second) % p;
first = second;
second = next;
number++;
}
return number;
}
// Driver code
int main()
{
int p = 7;
cout << "Minimal zero is: "
<< findMinZero(p) << endl;
return 0;
}
Java
// Java program to find minimal 0 Fibonacci
// for a prime number p
import java.io.*;
class FibZero
{
// Function that returns position of first Fibonacci number
// whose modulo p is 0
static int findMinZero(int p)
{
int first = 1, second = 1, number = 2, next = 1;
while (next > 0)
{
// add previous two remainders and
// then take its modulo p.
next = (first + second) % p;
first = second;
second = next;
number++;
}
return number;
}
// Driver program
public static void main (String[] args)
{
int p = 7;
System.out.println("Minimal zero is " + findMinZero(p));
}
}
Python3
# Python 3 program to find minimal
# 0 Fibonacci for a prime number p
# Returns position of first Fibonacci
# number whose modulo p is 0.
def findMinZero(p):
first = 1
second = 1
number = 2
next = 1
while (next):
next = (first + second) % p
first = second
second = next
number = number + 1
return number
# Driver code
if __name__ == '__main__':
p = 7
print("Minimal zero is:", findMinZero(p))
# This code is contributed by
# Surendra_Gangwar
C#
// C# program to find minimal 0
// Fibonacci for a prime number p
using System;
class GFG {
// Function that returns position
// of first Fibonacci number
// whose modulo p is 0
static int findMinZero(int p)
{
int first = 1, second = 1;
int number = 2, next = 1;
while (next > 0)
{
// add previous two
// remainders and then
// take its modulo p.
next = (first + second) % p;
first = second;
second = next;
number++;
}
return number;
}
// Driver program
public static void Main ()
{
int p = 7;
Console.WriteLine("Minimal zero "
+ "is :" + findMinZero(p));
}
}
// This code is contributed by anuj_67.
PHP
Javascript
输出:
Minimal zero is: 8