Gautam Sagar
Gautam Sagar

Reputation: 63

Code for finding Pisano Period showing wrong output

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)) 

Upvotes: 0

Views: 559

Answers (1)

Blender
Blender

Reputation: 298392

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...

Upvotes: 1

Related Questions