Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.19 KB | None | 0 0
  1. #Week 10 projects:
  2. #Patterns in primes
  3.  
  4. import matplotlib.pyplot as plt
  5.  
  6. n = 100
  7. #1)
  8. #a)
  9. def primelist(n):
  10. prime_list = list(range(2,n+1))
  11. for i in prime_list:
  12. j=2
  13. while i*j <= prime_list[-1]:
  14. if i*j in prime_list:
  15. prime_list.remove(i*j)
  16. j = j+1
  17. return prime_list
  18.  
  19. #primes = primelist(N)
  20.  
  21. #b)
  22. def prime_test(n):
  23. for i in range(2, n):
  24. if n % i == 0:
  25. return False
  26. return True
  27.  
  28. #c)
  29. def twinprimes(n):
  30. twinprimes_list = []
  31. for i in range(2,n):
  32. j = i+2
  33. if(prime_test(i) and prime_test(j)):
  34. twinprimes_list.append((i,j))
  35. return twinprimes_list
  36.  
  37. def twinprimes_sing(n):
  38. twinprimes_sing_list = []
  39. for i in range(2,n):
  40. j = i+2
  41. if(prime_test(i) and prime_test(j)):
  42. twinprimes_sing_list.append(j)
  43. return twinprimes_sing_list
  44.  
  45. #d)
  46. def sgprimes(n):
  47. sgprimes_list = []
  48. for i in range(2,n):
  49. j = 2*i+1
  50. if(prime_test(i) and prime_test(j)):
  51. sgprimes_list.append(i)
  52. return sgprimes_list
  53.  
  54. #e)
  55. #Non Logarithmic Plots:
  56. a = 20000
  57. fig1, ax1 = plt.subplots()
  58.  
  59. x = primelist(a)
  60. y = [i+1 for i in range(len(primelist(a)))]
  61. ax1.plot(x,y, '-b', label = 'Primes')
  62. ax1.legend(loc = 'upper left')
  63.  
  64. x = twinprimes_sing(a)
  65. y = [i+1 for i in range(len(twinprimes_sing(a)))]
  66. ax1.plot(x,y, '-r', label = 'Twin Primes')
  67. ax1.legend(loc = 'upper left')
  68.  
  69. x = sgprimes(a)
  70. y = [i+1 for i in range(len(sgprimes(a)))]
  71. ax1.plot(x,y, '-', color = 'orange', label = 'Sophie Germain Primes')
  72. ax1.legend(loc = 'upper left')
  73.  
  74. plt.xlabel('n')
  75. plt.ylabel('Respective Prime Counts')
  76. plt.title('Prime/Twin Prime/SG Prime Counts')
  77. fig1.show()
  78.  
  79.  
  80. #Logarithmic Plots:
  81. fig2, ax2 = plt.subplots()
  82.  
  83. x = primelist(a)
  84. y = [i+1 for i in range(len(primelist(a)))]
  85. ax2.loglog(x,y, '-b', label = 'Primes')
  86. ax2.legend(loc = 'upper left')
  87.  
  88. x = twinprimes_sing(a)
  89. y = [i+1 for i in range(len(twinprimes_sing(a)))]
  90. ax2.loglog(x,y, '-r', label = 'Twin Primes')
  91. ax2.legend(loc = 'upper left')
  92.  
  93. x = sgprimes(a)
  94. y = [i+1 for i in range(len(sgprimes(a)))]
  95. ax2.loglog(x,y, '-', color = 'orange', label = 'Sophie Germain Primes')
  96. ax2.legend(loc = 'upper left')
  97.  
  98. plt.xlabel('log(n)')
  99. plt.ylabel('Respective Prime Counts')
  100. plt.title('Prime/Twin Prime/SG Prime Counts (Logarithmic)')
  101. fig2.show()
  102.  
  103.  
  104. #2
  105. #a)
  106. def sexyprimes(n):
  107. sexyprimes_list = []
  108. for i in range(2,n):
  109. j = i+6
  110. if(prime_test(i) and prime_test(j)):
  111. sexyprimes_list.append((i,j))
  112. return sexyprimes_list
  113.  
  114. #b)
  115. def tripletprimes(n):
  116. tripletprimes_list = []
  117. for i in range(2,n):
  118. j,k,l = i+2,i+4,i+6
  119. if(prime_test(i) and prime_test(j) and prime_test(l)):
  120. tripletprimes_list.append((i,j,l))
  121. if(prime_test(i) and prime_test(k) and prime_test(l)):
  122. tripletprimes_list.append((i,k,l))
  123. return tripletprimes_list
  124.  
  125. #c)
  126. def tripletprimes_count(n): return len(tripletprimes(n))
  127. def sexyprimes_count(n): return len(sexyprimes(n))
  128. def twinprimes_count(n): return len(twinprimes(n))
  129.  
  130. def S(n):
  131. if sexyprimes_count(n) != 0:
  132. return tripletprimes_count(n)/sexyprimes_count(n)
  133. def T(n):
  134. if twinprimes_count(n) != 0:
  135. return tripletprimes_count(n)/twinprimes_count(n)
  136.  
  137. def S_output(n):
  138. listy = []
  139. for i in range(1,n):
  140. x = S(i)
  141. if S(i) != S(i-1):
  142. listy.append(x)
  143. return listy
  144.  
  145. def S_list(n):
  146. listx = []
  147. for i in range(1,n):
  148. if S(i) != S(i-1):
  149. listx.append(i)
  150. return listx
  151.  
  152. def T_output(n):
  153. listy = []
  154. for i in range(1,n):
  155. x = T(i)
  156. if T(i) != T(i-1):
  157. listy.append(x)
  158. return listy
  159.  
  160. def T_list(n):
  161. listx = []
  162. for i in range(1,n):
  163. if T(i) != T(i-1):
  164. listx.append(i)
  165. return listx
  166. '''
  167. fig3, ax3 = plt.subplots()
  168. fig4, ax4 = plt.subplots()
  169.  
  170. S_T_value
  171.  
  172. x_2 = T_list(200)
  173. y_2 = T_output(200)
  174. plot_c = ax3.plot(x_2,y_2,label = 'T(n) against n.')
  175.  
  176. x_1 = S_list(500)
  177. y_1 = S_output(500)
  178. plot_d = ax4.plot(x_1,y_1,label = 'S(n) against n.')
  179.  
  180. fig3.show()
  181. fig4.show()
  182. '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement