Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Problem with ifs and fors

Name: Anonymous 2011-08-14 7:18

I've only recently started trying to learn Python; I'm using Project Euler for some practice, and I'm currently doing Problem 24

I'm sure my code is not the most efficient out there, and it looks quite silly, but it gives me the correct answer and doesn't take too long, so I'm happy with it for now.

I have it printing out the correct answer, but then the program is still running, working out the next numbers in the sequence. I'd appreciate any help in getting my program to stop running once it's output the correct answer. Here is my code:

for b in range(0,10):
    if b != a:
        for c in range(0,10):
            if c not in (a,b):
                for d in range(0,10):
                    if d not in (a,b,c):
                        for e in range (0,10):
                            if e not in (a,b,c,d):
                                for f in range(0,10):
                                    if f not in (a,b,c,d,e):
                                        for g in range(0,10):
                                            if g not in (a,b,c,d,e,f):
                                                for h in range(0,10):
                                                    if h not in (a,b,c,d,e,f,g):
                                                        for i in range(0,10):
                                                            if i not in (a,b,c,d,e,f,g,h):
                                                                for j in range(0,10):
                                                                    if j not in (a,b,c,d,e,f,g,h,i):
                                                                        z = 1000000000*a + 100000000*b + 10000000*c + 1000000*d + 100000*e + 10000*f + 1000*g + 100*h + 10*i + j
                                                                        m = m + 1
                                                                        if m == 1000000:
                                                                            print z

Name: Anonymous 2011-08-14 23:50

Not everything has to be solved with a billion nested loops or deep recursion.

For ten digits, there are 9! = 362880 permutations beginning with a given digit.  Therefore the 1000000th permutation will start with 2 since 2*362880 < 1000000 < 3*362880.  Thus we're looking for the 1000000-2*362880 = 274240th permutation starting with 2.

The remaining digits are 0,1,3,4,5,6,7,8,9.

For 9 digits, there are 8! = 40320 permutations starting with a given digit (note that it doesn't matter that the 9 digits in this case aren't 0,1,2,3,4,5,6,7,8).  Since 6*40320 < 274240 < 7*40320, the next digit is the 7th lowest remaining digit, i.e. 7, and we want the 274240 - 6*40320 = 32320th permutation starting with 27.

same thing, blah blah, 6*7! < 32320 < 7*7!, so the next digit is the 7th lowest digit of 0,1,3,4,5,6,8,9, which is 8.

Keep going to get the final answer, I can't be bothered.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List