AtkinsSJ

Project Euler 35 (Broken)

May 21st, 2011
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.91 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. # http://projecteuler.net/index.php?section=problems&id=48
  4.  
  5. import time, math, sieve
  6. start = time.time()
  7.  
  8. def rotations(n):
  9.     '''Returns all rotated values for n, as a set'''
  10.     results = set()
  11.     string = str(n)
  12.     for i in range(len(string)):
  13.         string = string[1:] + string[0]
  14.         results.add(int(string))
  15.    
  16.     return results
  17.  
  18. # Get primes
  19. primes = set(sieve.primeSieve(1000000))
  20. results = set()
  21.  
  22. # For each prime:
  23. while len(primes):
  24.     # check primes for all the rotations
  25.     p = primes.pop()
  26.     primes.add(p)
  27.     rots = rotations(p)
  28.     print 'Rotations: ' + `rots`
  29.    
  30.     if rots.issubset(primes):
  31.         # if they're all present, put them in results
  32.         results = results.union(rots)
  33.     # remove all the rotations from primes
  34.     for r in rots:
  35.         primes.discard(r)
  36.  
  37. total = len(results)
  38.  
  39. end = time.time()
  40. print 'The total is ' + `total`
  41. print 'This took ' + `end-start` + ' seconds to calculate.'
Advertisement
Add Comment
Please, Sign In to add comment