Advertisement
OreganoHauch

Non-prime difficult to divide numbers

Apr 29th, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3.  
  4. N = 200
  5.  
  6. def is_prime(n):
  7. i=2
  8. if n<i:
  9. return False
  10. for x in range(i, n):
  11. if n%x==0:
  12. return False
  13. return True
  14.  
  15. prime_numbers = []
  16. for i in range(1,N):
  17. prime = is_prime_alternative(i)
  18. if prime:
  19. prime_numbers.append(i)
  20.  
  21.  
  22. def lowest_divisor(x):
  23. if x < 2:
  24. print("Error. Number too small.")
  25. if x in [2,3,4,5]:
  26. return [2,3,2,5][x-2]
  27. lowest_divisor = 1
  28. for i in range(2,x//2):
  29. if x%i == 0:
  30. lowest_divisor = i
  31. return lowest_divisor
  32. if i == x//2-1:
  33. return x
  34.  
  35.  
  36. ratiolist = []
  37. for i in range(2,N):
  38. ratio = float(i)/float(lowest_divisor(i))
  39. ratiolist.append(ratio)
  40.  
  41. x = np.arange(2,N,1)
  42.  
  43. # x and y ("ratiolist") without the prime numbers:
  44.  
  45. x_filtered = [value for value in x if not is_prime(value)]
  46. ratiolist_filtered = []
  47. for i in range(2,N):
  48. if not is_prime(i):
  49. ratio = float(i)/float(lowest_divisor(i))
  50. ratiolist_filtered.append(ratio)
  51.  
  52. lowest_divisor_list = []
  53. for i in range(2,N):
  54. if not is_prime(i):
  55. lowest_divisor_list.append(lowest_divisor(i))
  56.  
  57. # plot
  58. fig0, ax0 = plt.subplots(figsize=(15,5))
  59. ax0.scatter(x_filtered,lowest_divisor_list)
  60. ax0.set_title('The lowest divisor.')
  61. plt.ylabel("lowest divisor for x", fontsize=12)
  62. fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(15,5))
  63. ax1.scatter(x, ratiolist)
  64. ax1.set_title('With prime numbers')
  65. ax1.set_ylabel("ratio of x to its lowest divisor", fontsize=12)
  66. ax2.scatter(x_filtered, ratiolist_filtered)
  67. ax2.set_title('Without prime numbers')
  68. ax2.set_ylabel("ratio of x to its lowest divisor", fontsize=12)
  69.  
  70.  
  71.  
  72.  
  73. # ignore this code snippet, it's from another project:
  74.  
  75. def intersection(l1, l2):
  76. l3 = [value for value in l1 if value in l2]
  77. return l3
  78. def overhang(l1, l2):
  79. l3 = [value for value in l1 if value not in intersection(l1,l2)]
  80. return l3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement