Created
June 26, 2017 04:12
-
-
Save arguiot/7aaf4324b2d023d233c5d2268ea9b268 to your computer and use it in GitHub Desktop.
Primes probability
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
import sympy as sp | |
import progressbar | |
def numOfPrimesBellow(n): | |
# primeArray = [] | |
# for i in range(n): | |
# if i in sp.sieve: | |
# primeArray.append(i) | |
# return primeArray | |
return list(sp.sieve.primerange(1, n)) | |
const = int(input("Input number: ")) | |
print("Latest value: " + str(len(numOfPrimesBellow(const))/const)) | |
if input("Do I graph? [y/n]") == "y": | |
x = np.arange(1, const) | |
y = [] | |
bar = progressbar.ProgressBar(widgets=[ | |
' [', progressbar.Timer(), '] ', | |
progressbar.Bar(), | |
' (', progressbar.ETA(), ') ', | |
]) | |
for i in x: | |
y.append(len(numOfPrimesBellow(i))/i) | |
if i % (const/100) == 0: | |
bar.update(i*100/const) | |
plt.plot(x,y) | |
plt.title("Probability of getting a prime number\nrandomly") | |
plt.savefig(str(input("Name of the fig: ")), dpi=int(input("dpi: "))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is the renders

