Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # http://projecteuler.net/index.php?section=problems&id=48
- import time, math, sieve
- start = time.time()
- def rotations(n):
- '''Returns all rotated values for n, as a set'''
- results = set()
- string = str(n)
- for i in range(len(string)):
- string = string[1:] + string[0]
- results.add(int(string))
- return results
- # Get primes
- primes = set(sieve.primeSieve(1000000))
- results = set()
- # For each prime:
- while len(primes):
- # check primes for all the rotations
- p = primes.pop()
- primes.add(p)
- rots = rotations(p)
- print 'Rotations: ' + `rots`
- if rots.issubset(primes):
- # if they're all present, put them in results
- results = results.union(rots)
- # remove all the rotations from primes
- for r in rots:
- primes.discard(r)
- total = len(results)
- end = time.time()
- print 'The total is ' + `total`
- print 'This took ' + `end-start` + ' seconds to calculate.'
Advertisement
Add Comment
Please, Sign In to add comment