我需要帮助做一件我正在做的工作。任务是编写一个程序来寻找两个给定值之间的所有Wieferich素数。确定它是否为Wieferich素数的公式如下: Wieferich素数p是这样的:p2除以2(p−1)−1
到目前为止,我的情况是:
start=int(input("enter start value"))
end=int(input("enter end value"))
for c in range(start,end):
if c%2!=0:
primedet=(c**2)/((2**(c-1))-1)
if primedet%1==0:
print(c," is a Wiefrich Prime")
每次我运行它,它只打印给定值之间的所有奇数。我知道只有两个Wieferich素数:1093和3011。我真的不知道该怎么做。任何指导都将不胜感激。你知道吗
这很简单。根据你的陈述,这些数字的性质是素数prime和Wieferich仅仅通过你给出的等式,所以(2(p-1)-1)%p2==0返回
True
意味着你找到了一个数字。如@Copperfield所述,这可以写成(2(p-1))%p2==1。然后你可以做(在pow的帮助下,速度更快):模运算的使用使这一任务变得更容易,因为您希望2p-1-1可以被p2整除,也就是说2p-1-1=0(modp2)重新排列,在python中得到2p-1=1(modp2)
但这是低效的,因为首先计算2p-1然后取模,但别担心,python有一种有效的方法,用pow的3参数调用进行模幂运算
最后,你还需要p是素数,然后实现素数测试,你就可以开始了
这就是你找到Wieferich prime所需要的一切
你的另一个错误就在这里
2c-1-1总是大于c2(到足够大的c),因此c2/(2c-1-1)<;1
此外
因为primedet是一个float,当你做float%1时,它会给你这个数字的小数部分,混合四舍五入问题,你会得到太多的零, 但更重要的是,你测试的不是Wieferich素数的定义。你知道吗
相关问题 更多 >
编程相关推荐