1

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
Jose Magana
  • 916
  • 2
  • 10
  • 24

1 Answers1

2

Try using the generator (xrange) instead of a list (range), i.e,

for i in xrange(1234567890, 9876543210 + 1): 
physcheng
  • 58
  • 5
  • I was actually running in 2.7.6, but running in 3.4.1 actually solved my problem. Does 3.4.1 automatically use a generator instead of malloc for the range? Is that why running in 3 would work? – Jose Magana Jul 29 '15 at 23:27
  • 1
    Python 3 uses a [different `range` entirely](http://stackoverflow.com/questions/30081275/why-is-1000000000000000-in-range1000000000000001-so-fast-in-python-3). Python 2's `range` (and even `xrange` to an extent) are basically naïve implementations. – TigerhawkT3 Jul 29 '15 at 23:35