Prime Number Algorithms
Various Python algorithms to find and list prime numbers.
- Sieve of Atkin (a fast, modern optimization of the Sieve of Erastosthenes)
- Efficiency, a custom script
- Sieve of Erastosthenes (the famous one)
- Trial Division, a very naive approach
- Sieve of Zakiya (a super-optimized algorithm created by a Yahoo programmer)
Created for the Pennsylvania Junior Academy of Science, 2013. For this project I implemented the above algorithms to find every prime number up to a certain limit and tested their runtimes.
When to use
Use these algorithms when you need to find a long list of prime numbers, or if you just need to find a large prime number. These algorithms were tested with numbers up to 10^7.
I normalized each algorithm's average runtime to assign it a 'score'; faster algorithms have lower scores.
|Algorithm||Score (lower is faster)|
|Sieve of Zakiya||2.244|
|Sieve of Erastosthenes||13.504|
|Sieve of Atkin||19.386|
- Basically, use the Sieve of Zakiya. It's not too hard to translate to other programming languages.
- The Sieve of Atkin is supposed to be an optimized version of the Sieve of Erastosthenes, but it's actually slower.
- Don't roll your own prime number finding algorithm.
- And please please please don't use trial division, where you divide every number n by every number from 1 to n/2 inclusive.