{Spam?} Re: [Tutor] Is there a better way to do this? (original) (raw)
Bob Gailer bgailer at alum.rpi.edu
Thu Jul 22 19:16:18 CEST 2004
- Previous message: [Tutor] Is there a better way to do this?
- Next message: [Tutor] Is there a better way to do this?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
At 07:45 PM 7/21/2004, jb wrote:
On Wed, Jul 21, 2004 at 09:22:26PM -0400, Hee-Seng Kye wrote: > I'm trying to write a program that computes six-digit numbers, in which > the left digit is always smaller than its following digit (i.e., it's > always ascending). The output of the program starts with "0 1 2 3 4 5" > and ends on "6 7 8 9 A B." The best I could do was to have many > embedded 'for' statements: > > c = 1 > for p0 in range(0, 7): > for p1 in range(1, 12): > for p2 in range(2, 12): > for p3 in range(3, 12): > for p4 in range(4, 12): > for p5 in range(5, 12): > if p0 < p1 < p2 < p3 < p4 < p5:_ _> print repr(c).rjust(3), "\t", > print "%X %X %X %X %X %X" % (p0, p1, p2, p3, p4, p5) > c += 1 > print "...Done" > > This works, except that it's very slow. I need to get it up to > nine-digit numbers, in which case it's significantly slower. I was > wondering if there is a more efficient way to do this. >
this version is a bit quicker: c = 1 for p0 in range(0, 7): for p1 in range(p0+1, 12): for p2 in range(p1+1, 12): for p3 in range(p2+1, 12): for p4 in range(p3+1, 12): for p5 in range(p4+1, 12): print repr(c).rjust(3), "\t", print "%X %X %X %X %X %X" % (p0, p1, p2, p3, p4, p5) c += 1 print "...Done"
When I run the loops without printing on my (I think 2 GHZ with 512 M ram) machine it takes .002 seconds. Adding the expressions used in the print statements without printing raises it to .01 seconds. Running with printing takes about 2.5 seconds. 925 numbers get created in the process.
What is your mileage? What is your processor speed/RAM? How are you running the program?
Bob Gailer bgailer at alum.rpi.edu 303 442 2625 home 720 938 2625 cell
- Previous message: [Tutor] Is there a better way to do this?
- Next message: [Tutor] Is there a better way to do this?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]