SHARE
TWEET

Untitled

a guest Aug 17th, 2019 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import matplotlib.pyplot as plt
  2.  
  3. import numpy
  4. def primesfrom2to(n):
  5.     """ Input n>=6, Returns a array of primes, 2 <= p < n """
  6.     sieve = numpy.ones(n//3 + (n%6==2), dtype=numpy.bool)
  7.     for i in range(1,int(n**0.5)//3+1):
  8.         if sieve[i]:
  9.             k=3*i+1|1
  10.             sieve[       k*k//3     ::2*k] = False
  11.             sieve[k*(k-2*(i&1)+4)//3::2*k] = False
  12.     return numpy.r_[2,3,((3*numpy.nonzero(sieve)[0][1:]+1)|1)]
  13.  
  14.  
  15. ones = {}
  16. for prime in primesfrom2to(1000000):
  17.     ones[prime] = bin(prime).count("1")
  18.     #print(f"{prime} -> {ones}")
  19.  
  20.  
  21. plt.plot(ones.keys(), ones.values(), label='linear')
  22. plt.show()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top