Advertisement
makispaiktis

Prime numbers in Ranges

Apr 29th, 2020 (edited)
603
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.83 KB | None | 0 0
  1. '''
  2. CHAPTER 1
  3. '''
  4.  
  5. def isPrime(n):
  6.     if n < 0 or int(n) != n:
  7.         return None
  8.     if n == 2:
  9.         return True
  10.     if n == 1 or n % 2 == 0:
  11.         return False
  12.     elif n % 2 == 1:
  13.         flag = True
  14.         for i in range(2, int(n/3)+1):
  15.             if n % i == 0:
  16.                 flag = False
  17.                 return flag
  18.         if flag:
  19.             return flag
  20.  
  21. # This function will calculate how many prime numbers are in the range [10^a, 10^b)
  22. def howManyPrimes(a, b):
  23.     if a < 0 or b < 0 or a != int(a) or b != int(b):
  24.         return None
  25.     primes = []
  26.     for i in range(10**a, 10**b):
  27.         if isPrime(i):
  28.             primes.append(i)
  29.     return len(primes)
  30.  
  31.  
  32. '''
  33. CHAPTER 2
  34. '''
  35.  
  36. # MAIN FUNCTION
  37. # Basic Data
  38. limitPowerOf10 = 4
  39.  
  40. # First, I want to show how many primes are in each specific logarithmic range
  41. # List 1
  42. primesInContinuousRanges = []
  43. for i in range(0, limitPowerOf10):
  44.     primesInContinuousRanges.append(howManyPrimes(i, i+1))
  45. print()
  46.  
  47. xlabels = []
  48. for i in range(0, limitPowerOf10):
  49.     string1 = str(10**i) + "-" + str(10**(i+1))
  50.     xlabels.append(string1)
  51. print("~~~~~~~~ Data 1 ~~~~~~~~")
  52. for i in range(0, len(primesInContinuousRanges)):
  53.     print("There are " + str(primesInContinuousRanges[i]) + " primes in range " + xlabels[i])
  54. print()
  55.  
  56. # Now, I have to find how many primes are in the range [1, 10^x]
  57. # List 2
  58. primesInWholeRanges = []
  59. for i in range(0, len(primesInContinuousRanges)):
  60.     primesInWholeRanges.append(sum(primesInContinuousRanges[0:i+1]))
  61.  
  62. xlabels2 = []
  63. for i in range(0, limitPowerOf10):
  64.     string1 = str(1) + "-" + str(10**(i+1))
  65.     xlabels2.append(string1)
  66. print("~~~~~~~~ Data 2 ~~~~~~~~")
  67. for i in range(0, len(primesInWholeRanges)):
  68.     print("There are " + str(primesInWholeRanges[i]) + " primes in range " + xlabels2[i])
  69. print()
  70.  
  71. # List 3
  72. # Last step is to calculate the appearance frequency of a prime in a whole range
  73. frequencies = []
  74. for i in range(len(primesInWholeRanges)):
  75.     frequencies.append(10**(i+1) / primesInWholeRanges[i])
  76. print("~~~~~~~~ Data 3 ~~~~~~~~")
  77. for i in range(len(frequencies)):
  78.     print("You can find 1 prime number out of " + str(frequencies[i]) + " single numbers in the range " + xlabels2[i])
  79. print()
  80.  
  81. '''
  82. CHAPTER 3
  83. '''
  84. import matplotlib.pyplot as plt
  85. plt.plot(xlabels, primesInContinuousRanges, 'o', label="Primes in specific range")
  86. plt.xlabel("Ranges in logarithmic scale")
  87. plt.ylabel("Number of primes in each range")
  88. plt.show()
  89.  
  90. plt.plot(xlabels2, primesInWholeRanges, 'o', label="Primes in a whole range")
  91. plt.xlabel("Ranges in logarithmic scale")
  92. plt.ylabel("Number of primes in a whole range")
  93. plt.show()
  94.  
  95. plt.plot(xlabels2, frequencies, 'r--', label="Appearance Frequency")
  96. plt.xlabel("Ranges in logarithmic scale")
  97. plt.ylabel("Appearance frequency of a prime in a whole range")
  98. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement