2017-04-18 22:57:17 +08:00
|
|
|
/**
|
|
|
|
* The Sieve of Eratosthenes is an algorithm use to find prime numbers,
|
|
|
|
* up to a given value.
|
2016-11-24 02:07:18 +08:00
|
|
|
* Illustration: https://upload.wikimedia.org/wikipedia/commons/b/b9/Sieve_of_Eratosthenes_animation.gif
|
2017-04-18 22:57:17 +08:00
|
|
|
* (This illustration is also in the github repository)
|
|
|
|
*
|
|
|
|
* @author Unknown
|
|
|
|
*
|
|
|
|
*/
|
2016-11-24 02:07:18 +08:00
|
|
|
public class FindingPrimes{
|
2017-04-18 22:57:17 +08:00
|
|
|
/**
|
|
|
|
* The Main method
|
|
|
|
*
|
|
|
|
* @param args Command line arguments
|
|
|
|
*/
|
2016-11-24 02:07:18 +08:00
|
|
|
public static void main(String args[]){
|
|
|
|
SOE(20); //Example: Finds all the primes up to 20
|
|
|
|
}
|
|
|
|
|
2017-04-18 22:57:17 +08:00
|
|
|
/**
|
|
|
|
* The method implementing the Sieve of Eratosthenes
|
|
|
|
*
|
|
|
|
* @param n Number to perform SOE on
|
|
|
|
*/
|
2016-11-24 02:07:18 +08:00
|
|
|
public static void SOE(int n){
|
|
|
|
boolean sieve[] = new boolean[n];
|
|
|
|
|
|
|
|
int check = (int)Math.round(Math.sqrt(n)); //No need to check for multiples past the square root of n
|
|
|
|
|
|
|
|
sieve[0] = false;
|
|
|
|
sieve[1] = false;
|
|
|
|
for(int i = 2; i < n; i++)
|
|
|
|
sieve[i] = true; //Set every index to true except index 0 and 1
|
|
|
|
|
|
|
|
for(int i = 2; i< check; i++){
|
|
|
|
if(sieve[i]==true) //If i is a prime
|
|
|
|
for(int j = i+i; j < n; j+=i) //Step through the array in increments of i(the multiples of the prime)
|
|
|
|
sieve[j] = false; //Set every multiple of i to false
|
|
|
|
}
|
|
|
|
for(int i = 0; i< n; i++){
|
|
|
|
if(sieve[i]==true)
|
|
|
|
System.out.print(i+" "); //In this example it will print 2 3 5 7 11 13 17 19
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|