This python script analyzes and compares three different approaches to calculating the list of primes up to a given number:
- The simplest and possibly worst algorithm
- A polished and more efficient version of the previous algorithm, but still naive
- The sieve of Eratosthenes algorithm
- The sieve of Atkin, which has a theoretical better performance than the other algorithms, but actually depends on the implementation used. See here for an efficient code implementation
the list of primes up to 10000 is computed 5 times for each algorithm and the execution time is measured and printed
Some profiling of the execution time are then computed and plotted with the help of matplotlib
Save the source code file prime_numbers.py
in a folder on your computer. Open a terminal and navigate to you folder with the command cd
, then run the command
python3 prime_numbers.py