Advertisement
Guest User

Primes for stackexchange

a guest
Dec 26th, 2011
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.49 KB | None | 0 0
  1. def isprime(n, smallprimes):
  2.     """Checks if n is prime.
  3.  
  4.    smallprimes should be the SORTED list of all primes,
  5.    including all that are <= square root of p"""
  6.  
  7.     for p in smallprimes:
  8.         if p*p > n:
  9.             return True
  10.         if n % p == 0:
  11.             return False
  12.  
  13.  
  14.     return True
  15. # Nice test case: isprime(2, [2])
  16.  
  17. def primes_up_to(m):
  18.     primes = []
  19.  
  20.     for n in xrange(2, m+1):
  21.         if isprime(n, primes):
  22.             primes.append(n)
  23.  
  24.     return primes
  25.  
  26. def range_sieve(lo, hi, smallprimes):
  27.     """Finds all primes p with lo <= p <= hi. smallprimes is as in 'isprime'
  28.  
  29.    hi & lo might be large, but hi-lo+1 miust fit into a long"""
  30.  
  31.     # So ugly! But SO FAST!! How??
  32.  
  33.     delta = hi-lo+1
  34.     iamprime = [True] * delta      # iamprime[i] says whether lo + i is prime
  35.     if lo<= 1:
  36.         iamprime[1 - lo] = False
  37.  
  38.     def sillyfun():      # For profiling & speed comparison
  39.         pass
  40.  
  41.     for p in smallprimes:
  42.         rem = lo % p
  43.         if rem == 0:
  44.             iamprime[0] = False
  45.         for i in xrange(p - rem, delta, p):
  46.             iamprime[i] = False
  47.             sillyfun()
  48.            
  49.         if p >= lo and p <= hi:
  50.             iamprime[p - lo] = True
  51.  
  52.     return [p + lo for (p, ami) in enumerate(iamprime) if ami]
  53.  
  54. def range_f1(lo, hi, smallprimes):
  55.     """Finds all primes p with lo <= p <= hi. smallprimes is as in 'isprime'
  56.  
  57.    hi & lo might be large, but hi-lo+1 miust fit into a long.
  58.    Does not use a function call to check prime-ness, twice as fast"""
  59.  
  60.     primes =[]
  61.     for i in xrange(hi-lo+1):
  62.         n = lo + i
  63.  
  64.         isprime = True
  65.         for p in smallprimes:
  66.             if n % p == 0:
  67.                 isprime = False
  68.                 break
  69.  
  70.         if isprime:
  71.             primes.append(n)
  72.  
  73.     return primes
  74.  
  75. def range_f2(lo, hi, smallprimes):
  76.     """Finds all primes p with lo <= p <= hi. smallprimes is as in 'isprime'
  77.  
  78.    hi & lo might be large, but hi-lo+1 must fit into a long.
  79.    Why so slow?"""
  80.  
  81.     primes =[]
  82.     for i in xrange(hi-lo+1):
  83.         n = lo + i
  84.  
  85.         try:
  86.             (1 for p in smallprimes if n % p ==0).next()
  87.         except StopIteration:
  88.             primes.append(n)
  89.            
  90.     return primes
  91.  
  92. if __name__ == "__main__":
  93.   from cProfile import run as prof
  94.   from math import sqrt
  95.  
  96.   N = 10 ** 9
  97.   rtN = int(sqrt(N))
  98.   delta = 10**5
  99.   sp=primes_up_to(rtN)
  100.  
  101. # len sp = 3401
  102. #range_**(N-delta,N,sp) should have 4832 elmts
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement