[Numpy-discussion] Is there a better way to do this?
Jeffery D. Collins
jcollins_boulder at earthlink.net
Wed Jul 21 18:49:10 CDT 2004
Hee-Seng Kye wrote:
> My question is not directly related to NumPy, but since many people
> here deal with numbers, I was wondering if I could get some help; it
> would be even better if there is a NumPy (or Numarray) function that
> takes care of what I want!
>
> 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 best I could do was to have many embedded
> 'for' statement:
>
> 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 slow. I was
> wondering if there is a more efficient way to do this.
>
> I would highly appreciate it if anyone could help.
This appears to give the same results and is significantly faster.
def vers1():
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"
>
> Many thanks.
>
> -Kye
>
--
Jeff
More information about the Numpy-discussion
mailing list