有没有办法将印刷品限制为仅真正的同余?

Is there a way to limit the print to only true congruences?

我正在编写这段代码来计算费马小定理,它可以正常工作。我唯一的问题是我希望它更有效率。有没有办法将印刷品限制为仅真正的同余?

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

这段代码甚至对我不起作用,它给出了一个错误: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]