要求求斐波那契数列F(n)除以10007所得余数 n是整数
def fabo(x):
a,b=1,1
for i in range(2,x):
a,b=b%10007,(a+b)%10007
return b
def fabo1(x):
a,b=1,1
for i in range(2,x):
a,b=b,(a+b)
return b
在F(n)<10007这两个结果等价,但是F(n)>10007的时候这两个为什么等价(我运行一下结果一样),我感觉我用数学解释不来,请求指导
def fabo(x):
a,b=1,1
for i in range(2,x):
a,b=b%10007,(a+b)%10007
return b
def fabo1(x):
a,b=1,1
for i in range(2,x):
a,b=b,(a+b)
return b
for i in range(1,20000):
if fabo(i)!=fabo1(i):
print(i,fabo(i))
print(i,fabo1(i))
print(i-1,fabo(i-1))
print(i-1,fabo1(i-1))
print(i-2,fabo(i-2))
print(i-2,fabo1(i-2))
break
21 939
21 10946
20 6765
20 6765
19 4181
19 4181
6765+4181
10946
10946%10007
939
x<21时,两函数相等的,
x=21时 a+b>10007就不相等了
<10007等价正常。>10007怎么等价了?没有等价啊
当F(n)<10007时,a,b=b%10007,(a+b)%10007,这一句不是取余数么。结果还是自己本身啊,跟下面一样