有没有办法来限制打印到唯一真正的同余?

问题描述 投票:3回答:1

我工作的这个代码来计算费马小定理和它的作品,因为它应该。我有唯一的问题是,我希望它更有效率。有没有办法来限制打印到唯一真正的同余?

for i in range (1,351):
    print i, [(2 << i - 2) % i == 1]
python number-theory modular-arithmetic
1个回答
1
投票

此代码甚至没有工作对我来说,它提供了一个错误: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]
© www.soinside.com 2019 - 2024. All rights reserved.