我正在尝试在Canopy python编辑器中的python2和python3中运行此python程序,而我慢学的Mac书需要大量的时间来运行它,我不确定它是否确实在做某事,或者只是暂停。
import binascii
lower = 10**70
upper = 10**80
v = (lower+upper)/2
c = 2205316413931134031046440767620541984801091216351222789180593875373829950860542792110364325728088504479780803714561464250589795961097670884274813261496112882580892020487261058118157619586156815531561455215290361274334977137261636930849125
while True:
p = pow(v, 3)
if p < c:
lower = v
elif p > c:
upper = v
else:
print(v)
binascii.unhexlify(hex(v)[2:])
exit()
我使用的数字很大,因此我尝试将变量缩短为:
lower = 10**7
upper = 10**8
v = (lower+upper)/2
c = 22053164
但是,即使数字较小,我也无法及时输出。恰好2分钟后,我中断了内核,以查看程序的溢出位置,这是我所学到的内容:
Python 2-停在第12行
Python 2在内核名称空间中运行-在第10行停止
Python 3-停在第10行
Python 3在内核名称空间中运行-在第9行停止
无论如何,我是否可以让程序每两分钟给我输出一次,以便确保它仍在运行?还是有什么我可以尝试使我的程序加速并仍然提供我需要的输出的东西?
答案 0 :(得分:0)
查看程序正在执行的最简单方法是在代码中插入一系列打印语句。
当您看到控制台显示的内容时,您将能够掌握变量所发生的情况。提示:p不会改变,并且会永远循环。
import binascii
lower = 10**70
upper = 10**80
v = (lower+upper)/2
c = 2205316413931134031046440767620541984801091216351222789180593875373829950860542792110364325728088504479780803714561464250589795961097670884274813261496112882580892020487261058118157619586156815531561455215290361274334977137261636930849125
print("Starting Loop")
while True:
p = pow(v, 3)
print("p value is now:", p)
if p < c:
lower = v
elif p > c:
upper = v
else:
print(v)
binascii.unhexlify(hex(v)[2:])
exit()
答案 1 :(得分:0)
您可以只跟踪挂墙时间并偶尔打印出一些内容:
import binascii
import time
TIME_INTERVAL = 5 # Seconds
lower = 10**70
upper = 10**80
v = (lower+upper)/2
c = 2205316413931134031046440767620541984801091216351222789180593875373829950860542792110364325728088504479780803714561464250589795961097670884274813261496112882580892020487261058118157619586156815531561455215290361274334977137261636930849125
begin_interval = time.time()
while True:
p = pow(v, 3)
if p < c:
lower = v
elif p > c:
upper = v
else:
print(v)
binascii.unhexlify(hex(v)[2:])
exit()
if time.time() - begin_interval > TIME_INTERVAL:
print('still running')
begin_interval = time.time() # Reset.