Speeding up a script -- looking for ideas

Richard Bow donkan7 at yahoo.com
Sun Oct 20 04:24:12 EDT 2002


I'm hoping to get some hints for significantly speeding up the below 
script. I've found 43 integers between 1 and 1,000,000 that satisfy the 
condition, and would like to push on toward 10,000,000 in hopes of finding 
an integer for which there are 3 pairs. Of course, another reason for 
asking is to learn more Python from you guys.

Thanks in advance,

Richard Bow

======================================================================
# This finds integers n for which there are 2 pairs of positive integers,
#(a,b) and (c,d) such that (a**3) + (b**3) = n, and (c**3) + (d**3) = n.
# see http://www.mathpages.com/home/kmath028.htm 

start = 1000000
end   = 2000000
 
import time
t1= time.time()

for n in range(start, end + 1):
    crn = int(n**(1/3.0)) + 2 #  because of float probs, add 2 to be sure 
    list = []                 #  crn is large enough 

    for a in range(1,crn):
        a3 = a ** 3 
        for b in range(a,crn): 
            b3 = b ** 3 
            if a3 + b3 == n:                        
                list = list + [(n,a,b)]
                if len(list) >= 2:
                # e.g. [(1729, 1, 12), (1729, 9, 10)] and 
			# [(994688, 29, 99), (994688, 60, 92)]

                    print list
                    print
                    
t2 = time.time()
print "Time's up!", t2 - t1, "seconds have elapsed"

print "EEEEEEEEEnnnnnnnnnnddddddddd"

##total for n = 1 thru n = 1,000,000 is 43 integers
==========================================================





More information about the Python-list mailing list