而不是打印矩阵,shell进入无限循环

时间:2016-10-04 04:28:28

标签: python-3.x fibonacci

这个关于Fibonacci矩阵指数的程序在尝试在函数中打印矩阵时会进入infite循环。 (那些被评论过的行)

def matmul(a,b,c,modulo):
    for i in range (0,2):
        for j in range (0,2):
            c[i][j]=0
            for k in range (0,2):
                c[i][j]=c[i][j]+(a[i][k]*b[k][j])
                c[i][j]=c[i][j]%modulo
    #print(c)
    return c            

def mat(z,n,result,modulo):
    result=[[1,0],[0,1]]
    temp=[[0 for j in range(2)] for i in range(2)]
    while(n>0):
        matmul(result,z,temp,modulo)
        #print(temp[0][0],temp[0][1],temp[1][0],temp[1][1])
        for i in range (0,2):
            for j in range (0,2):
                result[i][j] = temp [i][j]
        matmul(z,z,temp,modulo)
        for i in range (0,2):
            for j in range (0,2):
                z[i][j]=temp[i][j]
        n=n/2
    return result   

def fibo(n,modulo):
    z=[[1,1],[1,0]]
    result=[[0 for j in range(2)] for i in range(2)]
    if(n>2):
        result=mat(z,n-2,result,modulo)
        f=result[0][0]*1+result[0][1]*0;
    else:
        f=n-1
    return f    


if __name__ == "__main__":
    n = int(input("enter value for n > "))
    modulo = int(input("enter value for modulo > "))
    print (fibo(n,modulo))

0 个答案:

没有答案