I have to make a list of primes, and I found out that all primes are of the form 6n+1 or 6n+5 (however not all numbers of this form are prime). Thus, I use it to find primes more quickly as such:
is_prime(n):
for all i <= sqrt(n)
if i divides n: return False
return n >= 2
generate_prime(past_prime):
// Since we know the past prime is already of the form 6n+1 or 6n+5, we just need to add either 4 or 2 respectively to get to the next candidate prime
if past_prime mod 6 is 1: past_prime += 4 // Moves from 6n+1 to 6n+5
else: past_prime += 2 // Moves from 6n+5 to 6n+7->6(n+1)+1
while(True):
if(is_prime(past_prime)):
return past_prime
if past_prime mod 6 is 1: past_prime += 4
else: past_prime += 2
What would be the speed of this algorithm (in terms of big O)? How would it compare to other deterministic methods like the sieve Erasthones?