Skip to content

Instantly share code, notes, and snippets.

@ga-wolf

ga-wolf/sieve.md Secret

Created August 25, 2015 07:41
Show Gist options
  • Save ga-wolf/2490e4614ed1ac530d60 to your computer and use it in GitHub Desktop.
Save ga-wolf/2490e4614ed1ac530d60 to your computer and use it in GitHub Desktop.
The Sieve of Eratosthenes

The Sieve of Eratosthenes

The Sieve of Eratosthenes is a simple, ancient algorithm for finding all prime numbers up to any given limit.

Create your range, starting at two and ending at the given limit.

The algorithm consists of repeating the following over and over:

  • take the next available unmarked number in your list (it is prime)
  • remove all the multiples of that number (they are not prime)

Repeat until you don't have any possible primes left in your range.

When the algorithm terminates, all the numbers in the list that have not been removed are prime.

Do this in Javascript!

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