Reputation: 43
i am trying to run this script and its already run for 2 days. and still dont get answer. i tried long instead int. what am i supposed to do? what is the better way to work with big numbers like this?
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
thank you very much.
Upvotes: 0
Views: 67
Reputation: 261
as I know, the problem is the famous 3n+1 problem, which could be: when the number x % 2 == 0 then you should divide 2, but not 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
The reason why your code have been running for two days is that your function go into endless loop.
Because you only exit the loop when x or y equals to 1, when x = 2, x % 2 == 0, then you will do x / 4 which get 0, x = 0 then runs forever.
Based on your code, you could do as
while x > 1:
//do your things here
It is >, modified, same for y.
Upvotes: 3