Skip to content

Instantly share code, notes, and snippets.

@vlad-bezden
Created February 6, 2021 14:00
Show Gist options
  • Save vlad-bezden/a47415f5000c7f6bd5c03aa9dc1f7f3e to your computer and use it in GitHub Desktop.
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
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