Skip to content

Instantly share code, notes, and snippets.

@arguiot
Created June 26, 2017 04:12
Show Gist options
  • Save arguiot/7aaf4324b2d023d233c5d2268ea9b268 to your computer and use it in GitHub Desktop.
Save arguiot/7aaf4324b2d023d233c5d2268ea9b268 to your computer and use it in GitHub Desktop.
Primes probability
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: ")))
@arguiot
Copy link
Author

arguiot commented Jun 26, 2017

Here is the renders
prime_prob-100000
prime_probability

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment