Created
August 31, 2017 17:44
-
-
Save lucafmi/729f6516799163b1a167ea233abbb3c6 to your computer and use it in GitHub Desktop.
Sieve of Atkin - a basic java implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/****************************************************************************** | |
* Compilation: javac AtkinSieve.java | |
* Execution: java AtkinSieve [number] [verbose] | |
* @author: Luca F | |
* | |
* % java AtkinSieve 1000 | |
* Found 168 primes | |
* | |
* verbose to show all results | |
* This file is intended for didactic purpose, to demonstrate how | |
* Atkin - Bernstein sieve works | |
* | |
******************************************************************************/ | |
public class AtkinSieve { | |
public static void main(String[] args) { | |
// Check if args[0] is specified and if it's valid | |
int size = 1; | |
try{ | |
size = Integer.parseInt(args[0]); | |
}catch(Exception e){ | |
System.out.println("Error: "+e); | |
} | |
int count = 0; | |
// Create a boolean array to manage Atkin Sieve | |
boolean[] numbers = new boolean[size+1]; | |
// Create variables for temporary equations solutions | |
int z = 0; | |
// Setting 2,3,5 as primes | |
numbers[2] = true;numbers[3] = true;numbers[5] = true; | |
// instead of solving equations, we will generate results from x and y roots | |
// the maximum admitted value for y is inferred by x | |
// 1st equation | |
for(int x=1; x<=(int)Math.sqrt(size/4);x++) | |
for(int y=1;y<=(int)Math.sqrt(size-4*x*x);y++){ | |
z = 4 * x * x + y * y; | |
// First condition: if z1 modulo 60 gives one of these remainders, we flip the number status: | |
if( z % 60 == 1 || | |
z % 60 == 13 || | |
z % 60 == 17 || | |
z % 60 == 29 || | |
z % 60 == 37 || | |
z % 60 == 41 || | |
z % 60 == 49 || | |
z % 60 == 53 ) | |
numbers[z] = !numbers[z]; | |
} | |
// 2nd equation | |
for(int x=1; x<=(int)Math.sqrt(size/3);x++) | |
for(int y=1;y<=(int)Math.sqrt(size- 3*x*x);y++){ | |
z = 3 * x * x + y * y; | |
if( z % 60 == 7 || | |
z % 60 == 19 || | |
z % 60 == 31 || | |
z % 60 == 43 ) | |
numbers[z] = !numbers[z]; | |
} | |
// 3rd equation; for this one, we can assume that y must be < x, so the condition is inverted | |
for(int x=1; x<=(int)Math.sqrt(size);x++) | |
for(int y=(int)Math.sqrt(3*x*x-size);y<=(int)Math.sqrt(3)*x;y++){ | |
z = 3 * x * x - y * y; | |
if(z <= size){ | |
if( z % 60 == 11 || | |
z % 60 == 23 || | |
z % 60 == 47 || | |
z % 60 == 59 ) | |
numbers[z] = !numbers[z]; | |
}else{ | |
// System.out.println(z +" exceeds " + x + " "+y); | |
} | |
} | |
// removing al square multiples | |
for (int i = 5; i <= (int)Math.sqrt(size); i++) | |
for (int j = 1; j * i * i <= size; j++) | |
numbers[j * i * i] = false; | |
// We have done! Showing the result.. | |
for (int i = 1; i <= size; i++) { | |
if (numbers[i] == true) { | |
if(args.length == 2 && ("verbose").equals(args[1])) System.out.println(i); | |
count++; | |
} | |
} | |
System.out.println("Found " + count + " primes"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/****************************************************************************** | |
* Compilation: javac AtkinSieveTwin.java | |
* Execution: java AtkinSieveTwin [number] [verbose] | |
* @author: Luca F | |
* | |
* % java AtkinSieveTwin 1000 | |
* Found 35 twins | |
* | |
* verbose to show all results | |
* This file is intended for didactic purpose, to demonstrate how | |
* Atkin - Bernstein sieve works, with some simplifications for twin numbers | |
* (some module remainders are not needed because never generate a potential twin) | |
******************************************************************************/ | |
public class AtkinSieveTwin { | |
public static void main(String[] args) { | |
// Check if args[0] is specified and if it's valid | |
int size = 1; | |
try{ | |
size = Integer.parseInt(args[0]); | |
}catch(Exception e){ | |
System.out.println("Error: "+e); | |
} | |
int count = 0; | |
// Create a boolean array to manage Atkin Sieve | |
boolean[] numbers = new boolean[size+1]; | |
// Create variables for temporary equations solutions | |
int z = 0; | |
// Setting 2,3,5,7 as primes | |
numbers[2] = true;numbers[3] = true;numbers[5] = true;numbers[7] = true; | |
// instead of solving equations, we will generate results from x and y roots | |
// the maximum admitted value for y is inferred by x | |
// 1st equation | |
for(int x=1; x<=(int)Math.sqrt(size/4);x++) | |
for(int y=1;y<=(int)Math.sqrt(size-4*x*x);y++){ | |
z = 4 * x * x + y * y; | |
// First condition: if z1 modulo 60 gives one of these remainders, we flip the number status: | |
/* We can remove remainders which don't generate twins */ | |
if( z % 60 == 1 || | |
z % 60 == 13 || | |
z % 60 == 17 || | |
z % 60 == 29 || | |
z % 60 == 41 || | |
z % 60 == 49 || | |
z % 60 == 53 ) | |
numbers[z] = !numbers[z]; | |
} | |
// 2nd equation | |
/* We can remove remainders which don't generate twins */ | |
for(int x=1; x<=(int)Math.sqrt(size/3);x++) | |
for(int y=1;y<=(int)Math.sqrt(size- 3*x*x);y++){ | |
z = 3 * x * x + y * y; | |
if( z % 60 == 19 || | |
z % 60 == 31 || | |
z % 60 == 43 ) | |
numbers[z] = !numbers[z]; | |
} | |
// 3rd equation; for this one, we can assume that y must be < x, so the condition is inverted | |
for(int x=1; x<=(int)Math.sqrt(size);x++) | |
for(int y=(int)Math.sqrt(3*x*x-size);y<=(int)Math.sqrt(3)*x;y++){ | |
z = 3 * x * x - y * y; | |
if(z <= size){ | |
if( z % 60 == 11 || | |
z % 60 == 23 || | |
z % 60 == 47 || | |
z % 60 == 59 ) | |
numbers[z] = !numbers[z]; | |
}else{ | |
// System.out.println(z +" exceeds " + x + " "+y); | |
} | |
} | |
// removing al square multiples | |
for (int i = 5; i <= (int)Math.sqrt(size); i++) | |
for (int j = 1; j * i * i <= size; j++) | |
numbers[j * i * i] = false; | |
// We have done! Showing the result.. | |
for (int i = 1; i <= (size-2); i++) { | |
if (numbers[i] == true && numbers[i+2]==true) { | |
if(args.length == 2 && ("verbose").equals(args[1])) System.out.println(i+","+(i+2)); | |
count++; | |
} | |
} | |
System.out.println("Found " + count + " twins"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment