Skip to content

Instantly share code, notes, and snippets.

@stephnr
Last active January 2, 2016 04:48
Show Gist options
  • Save stephnr/8252553 to your computer and use it in GitHub Desktop.
Save stephnr/8252553 to your computer and use it in GitHub Desktop.
Prime number generators written in Java. Both Eratosthenes and Atkins Sieves are implemented within as functions. Feel free to use as is. Average run time: 1.5 seconds. * I advise not running the complex values "abnormally" higher than 50,000. Complexity is exponential.
import java.util.*;
public class Generate_PrimeTable {
public static void main(String args[]) {
int erato_complex = 50000;
ArrayList<Integer> erato_primes = eratosthenes_Sieve(erato_complex);
System.out.println("Eratosthenes Sieve:");
System.out.println("Complexity: " + erato_complex);
System.out.println("# of Primes in Prime Chart: " + erato_primes.size());
System.out.println("Largest Prime: " + erato_primes.get(erato_primes.size() - 1));
System.out.println(erato_primes);
int a_complex = 50000;
ArrayList<Integer> atkin_primes = atkins_Sieve(a_complex);
System.out.println("\nAtkins Sieve (Experimental):");
System.out.println("Complexity: " + a_complex);
System.out.println("# of Primes in Prime Chart: " + atkin_primes.size());
System.out.println("Largest Prime: " + atkin_primes.get(atkin_primes.size() - 1));
System.out.println(atkin_primes);
}
//SLOWEST
//Provides an exact amount of primes equal to max
//Has 100% accuracy to providing consecutive primes.
private static ArrayList<Integer> eratosthenes_Sieve(int num_Of_Primes) {
int limit = num_Of_Primes;
num_Of_Primes *= 4.5;
int[] primeTable = new int[num_Of_Primes];
primeTable[0] = 1;
for(int i = 4; i < num_Of_Primes; i += 2) primeTable[i] = 1;
for(int i = 6; i < num_Of_Primes; i += 3) primeTable[i] = 1;
for(int i = 10; i < num_Of_Primes; i += 5) primeTable[i] = 1;
for(int i = 14; i < num_Of_Primes; i += 7) primeTable[i] = 1;
ArrayList<Integer> primes = new ArrayList<Integer>();
for(int i = 1; i < num_Of_Primes; i++) {
if(primeTable[i] == 0) primes.add(i);
if(primes.size() == limit) break;
}
return primes;
}
//FASTEST
//Provides an array of primes. Limit has no influence on how many.
//Primes are not wholly consecutive
//Not perfect, Sometimes produces results that are not primes. (Ex. 85)
public static ArrayList<Integer> atkins_Sieve(int limit) {
ArrayList<Integer> primeTable = new ArrayList<Integer>();
boolean[] sieve = new boolean[limit + 1];
int limitSqrt = (int) Math.sqrt((double)limit);
Arrays.fill(sieve, false);
sieve[0] = false;
sieve[1] = false;
sieve[2] = true;
sieve[3] = true;
primeTable.add(1);
for(int x = 1; x <= limitSqrt; x++) {
for(int y = 1; y <= limitSqrt; y++) {
int n = (4 * x * x) + (y * y);
if(n <= limit && (n % 12 == 1 || n % 12 == 5)) sieve[n] = !sieve[n];
n = (3 * x * x) + (y * y);
if (n <= limit && (n % 12 == 7)) sieve[n] = !sieve[n];
n = (3 * x * x) - (y * y);
if (x > y && n <= limit && (n % 12 == 11)) sieve[n] = !sieve[n];
}
for (int n = 5; n <= limitSqrt; n++) if (sieve[n]) x = n * n;
for (int i = x; i <= limit; i += x) sieve[i] = false;
for (int i = 0, j = 0; i <= limit; i++) if (sieve[i]) primeTable.add(i);
}
return primeTable;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment