Advertisement
makispaiktis

Project Euler 35 - Circular Primes

May 18th, 2020 (edited)
1,238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.81 KB | None | 0 0
  1. '''
  2. The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime.
  3. There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97.
  4. How many circular primes are there below one million?
  5. '''
  6.  
  7.  
  8. '''
  9. CHAPTER 1 - CREATE THE PRIMES LIST
  10. '''
  11. import itertools
  12. from timeit import default_timer as timer
  13. from itertools import permutations
  14. from math import pow
  15.  
  16. LIMIT = int(pow(10, 5))
  17. primes = [2]
  18. start = timer()
  19. for i in range(3, LIMIT):
  20.     isIPrime = True
  21.     for prime in primes:
  22.         if i % prime == 0:
  23.             isIPrime = False
  24.             break
  25.     if isIPrime == True:
  26.         primes.append(i)
  27.  
  28. end = timer()
  29. timeInMillisForPrimes = 1000 * (end - start)
  30. print()
  31.  
  32. print("************************    PRIMES    ************************")
  33. print("I found " + str(len(primes)) + " primes in the range (1, " + str(LIMIT) + ").")
  34. print("These primes are: " + str(primes))
  35. print("~~~~ Time needed for primes: " + str(timeInMillisForPrimes) + " ms ~~~~")
  36.  
  37.  
  38.  
  39. '''
  40. CHAPTER 2 - AUXILIARY FUNCTION - FACTORIAL
  41. '''
  42. def factorial(n):
  43.     if n < 0 or int(n) != n:
  44.         return None
  45.     elif n == 0:
  46.         return 1
  47.     else:
  48.         return n * factorial(n-1)
  49.  
  50.  
  51. '''
  52. CHAPTER 3 - ISCIRCULAR FUNCTION
  53. '''
  54. def isCircular(number, primes):
  55.     # !!!!!!!!!!!!!!!!!!!!!!!!!!!
  56.     # !!!!!!!!!!!!!!!!!!!!!!!!!!!
  57.     # This line can be subtracted
  58.     if number not in primes:
  59.         print(str(number) + " cannot be circular prime, because it isn't a prime number")
  60.         return None
  61.     # So, number is prime
  62.     # All the primes with one digit (2, 3, 5, 7) are circular
  63.     if number / 10 < 1:
  64.         return True
  65.     else:
  66.         # Number is a 2,3,4,....-digit prime
  67.         # Convert it into a string
  68.         # Example with
  69.         # 0. number = 137
  70.         stringNumber = str(number)
  71.         # 1. stringNumber = "137"
  72.         listString = [stringNumber[i] for i in range(len(stringNumber))]
  73.         # 2. listString = ["1", "3", "7"]
  74.         intString = list()
  75.         for i in range(len(listString)):
  76.             try:
  77.                 intString.append(int(listString[i]))
  78.             except ValueError:
  79.                 return None
  80.         # 3. intString = [1, 3, 7]
  81.         n = len(intString)          # n = 3
  82.         # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  83.         # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  84.         # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  85.         # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  86.  
  87.         perms = list(permutations(intString))
  88.         # 4. perms = [(1, 3, 7), (1, 7, 3), (3, 1, 7), (3, 7, 1), (7, 1, 3), (7, 3, 1)]
  89.         permNumbers = []
  90.         for i in range(factorial(n)):
  91.             permNumber = 0
  92.             for j in range(n):
  93.                 permNumber += perms[i][j] * pow(10, n-1-j)
  94.             permNumbers.append(int(permNumber))
  95.         # 5. permNumbers = [137, 173, 317, 371, 713, 731]
  96.  
  97.         # Time to check
  98.         flag = True
  99.         for permNumber in permNumbers:
  100.             if permNumber not in primes:
  101.                 return False
  102.         return True
  103.  
  104.  
  105. '''
  106. CHAPTER 4 - MAIN FUNCTION
  107. '''
  108.  
  109. # MAIN FUNCTION
  110. circulars = []
  111. start2 = timer()
  112. for prime in primes:
  113.     if isCircular(prime, primes) == True:
  114.         circulars.append(prime)
  115. end2 = timer()
  116. timeInMillisForCirculars = 1000 * (end2 - start2)
  117.  
  118. print()
  119. print("************************    CIRCULARS    ************************")
  120. print("I found " + str(len(circulars)) + " circular primes in the range (1, " + str(LIMIT) + ").")
  121. print("These numbers are: " + str(circulars))
  122. print("~~~~ Time needed for circulars: " + str(timeInMillisForCirculars) + "ms ~~~~")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement