Skip to main content
deleted 18 characters in body; edited tags; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

How can I improve the speed of my Python program? Project Euler #14 - Longest Collatz sequence

I'm attempting some Project Euler problems in an attempt to learn Python, and while. 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.

HereHere is one example: http://projecteuler.net/problem=14.

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)

This is the program. It produces the correct answer, but it takes a long long time. How can I speed this up? Using Python, again, This is also in 32bit32-bit Windows.

How can I improve the speed of my Python program?

I'm attempting some Project Euler problems in an attempt to learn Python, and 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: http://projecteuler.net/problem=14

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)

This is the program. It produces the correct answer, but it takes a long long time. How can I speed this up? Using Python, again, in 32bit Windows.

Project Euler #14 - Longest Collatz sequence

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.

Tweeted twitter.com/#!/StackCodeReview/status/155113190005538816
Post Migrated Here from stackoverflow.com (revisions)
Source Link

How can I improve the speed of my Python program?

I'm attempting some Project Euler problems in an attempt to learn Python, and 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: http://projecteuler.net/problem=14

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)

This is the program. It produces the correct answer, but it takes a long long time. How can I speed this up? Using Python, again, in 32bit Windows.