Created
February 6, 2021 14:00
-
-
Save vlad-bezden/a47415f5000c7f6bd5c03aa9dc1f7f3e to your computer and use it in GitHub Desktop.
An example of how to find prime numbers using Python itertools.takewhile function
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 itertools import takewhile | |
from math import sqrt | |
def prime(n: int) -> bool: | |
tests = set(range(2, int(sqrt(n) + 1))) | |
non_factors = set(takewhile(lambda i: n % i != 0, tests)) | |
return tests == non_factors |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment