The algorithm is based on a sieve of Eratosthenes.
seed is an integer that is checked for roughness. primes - a list of primes is less than seed and counts contains the corresponding least multiple, which is greater than seed .
Think of counts as a list of βnextβ crossed out numbers, but only one for each constantly updated one. When finding the next largest multiple, if we get exactly seed , then this is not simple, so it resets the outer loop (using i=-1 ).
Only when we updated the list of large multiple values ββwithout meeting exactly the seed , can we conclude that seed is simple.
Here the code is a bit simplified and commented out:
number_of_primes = 100 seed = 1 primes = [] counts = [] while primes.size < number_of_primes seed += 1 i = 0 while i < primes.size
source share