0

I want to calculate Pisano Period for any integer 2 <= m <= 100000.

This code is currently not working for m>4, for m>4 output showing is 9.What am I doing wrong in the below code.

def fib(n):
    i=0
    f=[]
    while(i<=n):
        f.append(i)
        i+=1
    i=2
    while(i<=n):
        f[i]=f[i-1]+f[i-2]
        i+=1
    return f[n]

m=int(input())
j=2
p=[0,1,1,2]
while (p[j-1]!=1 and p[j-2]!=0):

    h=fib(j)%m
    p.append(h)
    j+=1

print(len(p)) 

1 Answers1

1

Your stopping condition is p[::-1] != p[0:], which checks if the list is palindromic. Your stopping condition should be if the list repeats.

One way to do it would be to use the recurrence relation representing the n-th Fibonacci number, which still holds modulo m. There are only two initial values, so...

Blender
  • 289,723
  • 53
  • 439
  • 496
  • how can i check that – Gautam Sagar Jul 06 '16 at 15:28
  • @GautamSagar: That's for you to figure out. I already gave one hint. Another would be to check if the list literally repeats itself, which means that the first half is equal to the second half. That'll be slower, but it still works. – Blender Jul 06 '16 at 15:29