提问人:Manth Kumar 提问时间:11/27/2022 更新时间:11/27/2022 访问量:81
如何让python代码运行得更快?(用于解决 Collatz 猜想)
How to make python code run faster? (For solving the Collatz Conjecture)
问:
def main_func():
def infinity():
n = 295147905179352825856
while True:
yield n
n+=1
i = 295147905179352825856
data = 0
def func(i, num, x, data):
if x == True:
print(i, num, "F", data)
#F means the number that can't be reduced to 1 is found
i = 0
else:
print("Not Successful")
check = 0
for i in infinity():
i+=1
data+=1
num=i
if i > 1:
while i > 1:
if i % 2 == 0:
i = int(i/2)
if i == 1:
#print(data, ".", i, num)
#print("T")
if data == 100000:
data = 0
check+=1
print (check, num)
#This is done to make the console print less numbers/text
#else:
#print ("Not reached 1 Lakh")
#T means the number can be reduced to 1
else:
i = int(3*i + 1)
if i != 1:
x = bool(True)
func (i, num, x, data)
print(i, num, "Eureka!")
else:
print("Try Again")
main_func()
我试图使用 Python 为 Collatz 猜想制定解决方案。此代码检查从 2^68 到无穷大(永无止境)的所有数字是否可以使用 n/2 或 3n+1 将其转换为 1。我怎样才能使检查过程更快?
答: 暂无答案
评论
int(295147905179352825860/2)
=147573952589676412928
.如果不检查整个除法,您就会知道这是不正确的:以 0 结尾的数字的一半以 0 或 5 结尾,从不以 8 结尾。