大数字的脚本需要几天时间

script with big numbers take days

我正在尝试 运行 这个脚本,它已经 运行 了 2 天。仍然没有得到答案。我尝试了 long 而不是 int。 我应该做些什么? 像这样处理大数字的更好方法是什么?

count=int(0)
sum1=int(1)
x=int(343543363925512)
y=int(98325112)
while x!=1:
    sum1=sum1+x
    if x%2!=0:
        x=x*3+1
    else:
         x=x/4

while y!=1:

    if y%2!=0:
        y=y*3+1
    else:
         y=y/4
    count=count+1

answer=int(sum1*count)
print answer

非常感谢。

据我所知,问题是著名的 3n+1 问题,它可能是: 当数字 x % 2 == 0 那么你应该除以 2,而不是 4。

count=int(0)
sum1=int(1)
x=int(343543363925512)
y=int(98325112)
while x!=1:
    sum1=sum1+x
    if x%2!=0:
        x=x*3+1
    else:
         x=x/2

while y!=1:

    if y%2!=0:
        y=y*3+1
    else:
         y=y/2
    count=count+1

answer=int(sum1*count)
print answer

你的代码运行两天是因为你的函数进入了死循环。

因为你只在 x 或 y 等于 1 时退出循环,当 x = 2,x % 2 == 0 时,你将执行 x / 4 得到 0,x = 0 然后永远运行。

根据你的代码,你可以这样做

while x > 1:
    //do your things here

是>,修改,y一样。