Reputation: 941
When I run this in terminal, I never see anything until 30 seconds after starting when it prints:
killed 9
After the first time, I added the print(i)
to line 23 to see what's going on, but it never even prints the first number. There is something going on here that I don't understand. After changing the for loop to very small numbers, it works fine. I can't change the range of the loop because these are the exact numbers I need to look at. What should I do?
6 def isPandigital(digits):
7 sortedDigits = sorted(digits)
8 numDigits = len(sortedDigits)
9
10 if len(set(sortedDigits)) != numDigits:
11 return False
12
13 for i in range(1, numDigits + 1):
14 if i != sortedDigits[i - 1]:
15 return False
16
17 return True
18
19 primes = [2, 3, 5, 7, 11, 13, 17]
20 total = 0
21
22 for i in range(1234567890, 9876543210 + 1):
23 print(i)
24 digits = [int(dig) for dig in str(i)]
25 numDigits = 10
26
27 if isPandigital(digits):
28 for i in range(numDigits - 3):
29 newNum = int(''.join(map(str, digits[i:i + 3])))
30 if not (newNum % primes[i] == 0):
31 break
32 else:
33 total += i
Upvotes: 1
Views: 96
Reputation: 68
Try using the generator (xrange) instead of a list (range), i.e,
for i in xrange(1234567890, 9876543210 + 1):
Upvotes: 2