Skip to content

Instantly share code, notes, and snippets.

@siddMahen
Created December 30, 2011 20:20
Show Gist options
  • Save siddMahen/1541326 to your computer and use it in GitHub Desktop.
Save siddMahen/1541326 to your computer and use it in GitHub Desktop.
Prints primes using 6x ± 1
# coding=UTF-8
import math
inv = 1
pc = 0
lmt = 1000000
while(inv < lmt):
pp = (6*inv) - 1
bool = 0
if((pow(2, pp-1) % pp) == 1):
print "6 * "+str(inv)+" (-) is prime"
bool = 1
pp += 2
if((pow(2, pp-1) % pp) == 1):
print "6 * "+str(inv)+" (+) is prime"
bool = 1
if(bool == 0):
print "6 * "+str(inv)+" (±) is not prime"
else:
pc += 1
inv += 1
print pc

Generating Prime Numbers Using 6x ± 1

Usage

To run this, just do:

python 6prime.py

Do note that the current limit is set pretty high, and might take a few hours to compute. If you would like to change that, change the lmt variable to something more suitable.

Results

Day 1

Letting this run over night, then going to compare the results to the prime number theorem approximation.

Day 2

It's the morning, still not finished the calculations. Almost done though. Let me make some predictions based on the prime number theory.

The prime number theorem for arithmetic progressions states that a progression a, a + n, a + 2n, a + 3n, … less than x, has approximately:

π(x) = (1/ϕ(n))Li(x)

primes. Which in this case is about:

π(x) = 2((1/2)Li(6000000))
     = 413076.504

prime numbers (multiplied by 2 as I tested both -1 and 1 for a). Let's see how the results compare.

Final Results

Well this was definitely underwhelming. Only 375341 numbers were prime. So much for a increased distribution of primes around 6x ± 1.

I did however, notice a few interesting things. Firstly, the gap between primes increased dramatically as the value of 6 * inv got larger. There were significant swabs of text where I would see 6-7 non-primes in a row. Remember, these are multiples of 6 ± 1, so I did not expect such large gaps of nothing. Also why is this not apparent in the lower numbers? Almost all of the primes between 1 - 100 can accurately be identified using the 6x ± 1 method... Interesting.

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