Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- CHAPTER 1
- '''
- def isPrime(n):
- if n < 0 or int(n) != n:
- return None
- if n == 2:
- return True
- if n == 1 or n % 2 == 0:
- return False
- elif n % 2 == 1:
- flag = True
- for i in range(2, int(n/3)+1):
- if n % i == 0:
- flag = False
- return flag
- if flag:
- return flag
- # This function will calculate how many prime numbers are in the range [10^a, 10^b)
- def howManyPrimes(a, b):
- if a < 0 or b < 0 or a != int(a) or b != int(b):
- return None
- primes = []
- for i in range(10**a, 10**b):
- if isPrime(i):
- primes.append(i)
- return len(primes)
- '''
- CHAPTER 2
- '''
- # MAIN FUNCTION
- # Basic Data
- limitPowerOf10 = 4
- # First, I want to show how many primes are in each specific logarithmic range
- # List 1
- primesInContinuousRanges = []
- for i in range(0, limitPowerOf10):
- primesInContinuousRanges.append(howManyPrimes(i, i+1))
- print()
- xlabels = []
- for i in range(0, limitPowerOf10):
- string1 = str(10**i) + "-" + str(10**(i+1))
- xlabels.append(string1)
- print("~~~~~~~~ Data 1 ~~~~~~~~")
- for i in range(0, len(primesInContinuousRanges)):
- print("There are " + str(primesInContinuousRanges[i]) + " primes in range " + xlabels[i])
- print()
- # Now, I have to find how many primes are in the range [1, 10^x]
- # List 2
- primesInWholeRanges = []
- for i in range(0, len(primesInContinuousRanges)):
- primesInWholeRanges.append(sum(primesInContinuousRanges[0:i+1]))
- xlabels2 = []
- for i in range(0, limitPowerOf10):
- string1 = str(1) + "-" + str(10**(i+1))
- xlabels2.append(string1)
- print("~~~~~~~~ Data 2 ~~~~~~~~")
- for i in range(0, len(primesInWholeRanges)):
- print("There are " + str(primesInWholeRanges[i]) + " primes in range " + xlabels2[i])
- print()
- # List 3
- # Last step is to calculate the appearance frequency of a prime in a whole range
- frequencies = []
- for i in range(len(primesInWholeRanges)):
- frequencies.append(10**(i+1) / primesInWholeRanges[i])
- print("~~~~~~~~ Data 3 ~~~~~~~~")
- for i in range(len(frequencies)):
- print("You can find 1 prime number out of " + str(frequencies[i]) + " single numbers in the range " + xlabels2[i])
- print()
- '''
- CHAPTER 3
- '''
- import matplotlib.pyplot as plt
- plt.plot(xlabels, primesInContinuousRanges, 'o', label="Primes in specific range")
- plt.xlabel("Ranges in logarithmic scale")
- plt.ylabel("Number of primes in each range")
- plt.show()
- plt.plot(xlabels2, primesInWholeRanges, 'o', label="Primes in a whole range")
- plt.xlabel("Ranges in logarithmic scale")
- plt.ylabel("Number of primes in a whole range")
- plt.show()
- plt.plot(xlabels2, frequencies, 'r--', label="Appearance Frequency")
- plt.xlabel("Ranges in logarithmic scale")
- plt.ylabel("Appearance frequency of a prime in a whole range")
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement