OreganoHauch

IsPrime

Apr 28th, 2020
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. import time
  2.  
  3. def IsPrime(n):
  4. start = time.time()
  5. if n == 2:
  6. end = time.time()
  7. print("The function took", str(end-start), "seconds.")
  8. return True
  9. if n == 3:
  10. end = time.time()
  11. print("The function took", str(end-start), "seconds.")
  12. return True
  13. if n == 4:
  14. end = time.time()
  15. print("The function took", str(end-start), "seconds.")
  16. return False
  17. if n == 5:
  18. end = time.time()
  19. print("The function took", str(end-start), "seconds.")
  20. return True
  21. if n == 6:
  22. end = time.time()
  23. print("The function took", str(end-start), "seconds.")
  24. return False
  25. if n > 6:
  26. bool = []
  27. for i in range(2, n//2+1):
  28. if n%i == 0:
  29. bool.append("True")
  30. if "True" in bool:
  31. end = time.time()
  32. print("The function took", str(end-start), "seconds.")
  33. return False
  34. else:
  35. end = time.time()
  36. print("The function took", str(end-start), "seconds.")
  37. return True
  38. else:
  39. end = time.time()
  40. print("The function took", str(end-start), "seconds.")
  41. return False
  42.  
  43.  
  44.  
  45. def IsPrime_alternative(n):
  46. start = time.time()
  47. i=2
  48. if n<i:
  49. end = time.time()
  50. print("The function took", str(end-start), "seconds.")
  51. return False
  52. while i < n:
  53. if n%i==0:
  54. end = time.time()
  55. print("The function took", str(end-start), "seconds.")
  56. return False
  57. else:
  58. i+=1
  59. end = time.time()
  60. print("The function took", str(end-start), "seconds.")
  61. return True
  62.  
  63. difficult_to_divide_list = []
  64. # check for a random number x if it is hard to divide (i.e. divisible only by numbers higher than 8). If yes, return True, else: return False
  65. def check_boolean(x):
  66. bool = False # assume that it is not hard to divide
  67.  
  68. while ~bool:
  69.  
  70. if x%2 != 0:
  71. bool = True
  72. else:
  73. bool = False
  74. break
  75. if x%3 != 0:
  76. bool = True
  77. else:
  78. bool = False
  79. break
  80. if x%4 != 0:
  81. bool = True
  82. else:
  83. bool = False
  84. break
  85. if x%5 != 0:
  86. bool = True
  87. else:
  88. bool = False
  89. break
  90. if x%6 != 0:
  91. bool = True
  92. else:
  93. bool = False
  94. break
  95. if x%7 != 0:
  96. bool = True
  97. else:
  98. bool = False
  99. break
  100. if x%8 != 0:
  101. bool = True
  102. else:
  103. bool = False
  104. break
  105. break
  106. return bool
  107.  
  108.  
  109. N = 1000
  110.  
  111. prime_list = []
  112. for i in range(1,N):
  113. if IsPrime_alternative(i):
  114. prime_list.append(i)
  115.  
  116. def add_to_hard_divisors_list(N):
  117. for i in range(2,N):
  118. if check_boolean(i):
  119. difficult_to_divide_list.append(i)
  120.  
  121. add_to_hard_divisors_list(N)
  122.  
  123.  
  124. def intersection(l1, l2):
  125. l3 = [value for value in l1 if value in l2]
  126. return l3
  127. def overhang(l1, l2):
  128. l3 = [value for value in l1 if value not in intersection(l1,l2)]
  129. return l3
  130.  
  131. print("\n"+"This is the list of prime numbers: ")
  132. print(prime_list)
  133. print("\n"+"This is the list of difficult to divide numbers: ")
  134. print(difficult_to_divide_list)
  135. print("\n"+"This is the list of the junction: ")
  136. print(intersection(difficult_to_divide_list, prime_list))
  137. print("\n"+"This is the list of difficult to divide non-prime numbers: ")
  138. print(overhang(difficult_to_divide_list, prime_list))
Add Comment
Please, Sign In to add comment