【发布时间】:2019-02-09 01:29:14
【问题描述】:
我正在编写这段代码来计算费马小定理,它可以正常工作。我唯一的问题是我希望它更有效率。有没有办法将打印限制为只有真正的一致性?
for i in range (1,351):
print i, [(2 << i - 2) % i == 1]
【问题讨论】:
标签: python number-theory modular-arithmetic
我正在编写这段代码来计算费马小定理,它可以正常工作。我唯一的问题是我希望它更有效率。有没有办法将打印限制为只有真正的一致性?
for i in range (1,351):
print i, [(2 << i - 2) % i == 1]
【问题讨论】:
标签: python number-theory modular-arithmetic
此代码甚至对我不起作用,它给出了一个错误:ValueError: negative shift count。
但考虑到它以某种方式为您工作,您可以使用if 条件仅在为真时打印:
for i in range (2,351): # changing 1 to 2 fixed the error for me.
if (2 << i-2) % i == 1: # this will check if it's true, then only print
print i, [(2 << i - 2) % i == 1]
【讨论】: