Created
March 27, 2021 08:44
-
-
Save H2CO3/cb3516960cca6ecb9c57b9c3dced0b54 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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