首页 > 解决方案 > 有没有办法将打印限制为只有真正的一致性?

问题描述

我正在研究这段代码来计算费马小定理,它应该可以正常工作。我唯一的问题是我希望它更有效率。有没有办法将打印限制为只有真正的一致性?

for i in range (1,351):
    print i, [(2 << i - 2) % i == 1]

标签: pythonnumber-theorymodular-arithmetic

解决方案


这段代码甚至对我不起作用,它给出了一个错误:ValueError: negative shift count. 但考虑到它以某种方式为您工作,您可以使用if条件仅在 true 时打印:

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]

推荐阅读