I'm attempting some Project Euler problems in an attempt to learn Python. While I can get the correct answer quite easily, my programs are quite slow. Project Euler, if doing it correctly, specifies that each program should run in <1 minute. Some of mine take 5-10 mins.
Here is one example.
x=1;
terms=0;
maxTerms=0;
maxTermsNum=0;
for x in range(1,1000000):
i=x;
while (i!=1):
terms+=1;
if (i%2==0):
i/=2
else:
i=3*i+1
if (terms>maxTerms):
maxTerms=terms;
maxTermsNum=x;
print(x);
terms=0;
print("Biggest one: is %s." %maxTermsNum)
It produces the correct answer, but it takes a long long time. How can I speed this up? This is also in 32-bit Windows.
if
statements. More spacing would also make this look cleaner (i = x
instead ofi=x
), although that is more opinion. Check out Python's style guide for reference. \$\endgroup\$