Skip to content

Instantly share code, notes, and snippets.

@H2CO3
Created March 27, 2021 08:44
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 H2CO3/cb3516960cca6ecb9c57b9c3dced0b54 to your computer and use it in GitHub Desktop.
Save H2CO3/cb3516960cca6ecb9c57b9c3dced0b54 to your computer and use it in GitHub Desktop.
from math import floor
import numpy as np
def primes_upto(n):
maxdiv = floor(n ** 0.5) + 1
divs = np.arange(2, maxdiv)
nums = np.arange(2, n + 1)
rem = np.mod.outer(nums, divs)
mask = np.less.outer(nums, divs ** 2)
isprime = np.all(mask | rem, axis=1)
return np.argwhere(isprime).ravel() + 2
if __name__ == '__main__':
print(primes_upto(100))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment