Skip to content

Instantly share code, notes, and snippets.

@alexcasalboni
Created September 30, 2019 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexcasalboni/b8a33630544ca2ec1002a0b874c6cf9d to your computer and use it in GitHub Desktop.
Save alexcasalboni/b8a33630544ca2ec1002a0b874c6cf9d to your computer and use it in GitHub Desktop.
Primes Test with AWS Lambda
import json
import numpy as np
def compute_primes_up_to(n):
# https://stackoverflow.com/questions/2068372/fastest-way-to-list-all-primes-below-n-in-python/3035188#3035188
""" Input n>=6, Returns a array of primes, 2 <= p < n """
sieve = np.ones(int(n/3) + (n%6==2), dtype=np.bool)
sieve[0] = False
for i in range(int(int(n**0.5)/3+1)):
if sieve[i]:
k=3*i+1|1
sieve[ int((k*k)/3) ::2*k] = False
sieve[int((k*k+4*k-2*k*(i&1))/3)::2*k] = False
return np.r_[2,3,((3*np.nonzero(sieve)[0]+1)|1)]
def lambda_handler(event, context):
for i in range(1000):
primes = compute_primes_up_to(1000000)
print("Found %s primes" % len(primes))
return {
'OK': 'OK'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment