Modulo循环在23阶乘失败

时间:2018-02-17 05:29:28

标签: python-3.x factorial

以下代码一直工作到22!但23岁就失败了!

你能看到任何会导致意外结果的任何四舍五入吗?

它的作用:
findprimes - >返回素数列表
因子 - >计算"数字!' (一个因子)
击穿 - >返回一个字符串(一个等式),显示一个等于阶乘

结果的素数乘积
def findprimes(num):
    nonprimes=[0,1]
for i in range(num+2):
    for k in range((i)):
        if(i+2)%(k+2)==0:
            nonprimes.append(i+2)
return [c for c in range(i) if c not in set(nonprimes)]

def factor(num):
    factorial=1
    for i in range(num):
        factorial=factorial*(i+1)
    return factorial

def breakdown(num):
    answer=[]
    iter_me=findprimes(num)
    remains=factor(num)
    for i in range(len(iter_me)):
        k=0
        tot=0
        while remains%iter_me[i]==0:
            remains=int(remains/iter_me[i])
            k+=1
        if k>1:
#            print(k)
            answer.append(str(str(iter_me[i])+'^'+str(k)+' * '))
        else:
            answer.append(str(str(iter_me[i])+' * '))

    return ''.join(answer).rstrip('* ')


#breakdown lower that 23 works!!!
print(breakdown(23))

1 个答案:

答案 0 :(得分:1)

>  remains=int(remains/iter_me[i])

应该是

remains=int(remains//iter_me[i])