35.py 870 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. from math import sqrt
  2. def prime(n):
  3. """Returns a boolean indicating whether an integer is a prime"""
  4. if n <= 1:
  5. return False
  6. for i in range(2, int(sqrt(n) + 1)):
  7. if n % i == 0:
  8. return False
  9. return True
  10. def rotate(n):
  11. """Returns a list of all rotations of a number"""
  12. n = str(n)
  13. rotationslist = []
  14. rot = n
  15. while rot not in rotationslist:
  16. rotationslist.append(rot)
  17. rot = rot[-1] + rot[:-1]
  18. return map(int,rotationslist)
  19. def isCircularPrime(n):
  20. """Returns boolean whether all rotations of digits are prime"""
  21. rotations = rotate(n)
  22. for permutation in rotations:
  23. if prime(permutation) == False:
  24. return False
  25. return True
  26. def main(n):
  27. """Main thread for the program"""
  28. numberCircularPrimes = 0
  29. for i in range(n):
  30. if prime(i):
  31. if isCircularPrime(i):
  32. numberCircularPrimes += 1
  33. return numberCircularPrimes
  34. print main(1000000)