Skip to content

Instantly share code, notes, and snippets.

@mthpower
Created May 11, 2016 19:53
Show Gist options
  • Save mthpower/d607258bca540b75afc9534f52eae282 to your computer and use it in GitHub Desktop.
Save mthpower/d607258bca540b75afc9534f52eae282 to your computer and use it in GitHub Desktop.
def sieve(limit):
candidates = set(range(2, limit + 1))
for i in candidates:
sieved = set(filter(lambda x: not x % i, candidates - {i}))
candidates = candidates - sieved
return sorted(candidates)
print(sieve(997))
@tomviner
Copy link

It turns out you're looping over all the original candidate set.

Some different approaches to avoid this https://gist.github.com/tomviner/ddb9474f96f8d0366f5f39bf53208a16 plus an even more concise version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment