From c97d806ba0393c24bdaf8cc4626623b33cc89011 Mon Sep 17 00:00:00 2001 From: Rian Gallagher Date: Wed, 23 Nov 2016 18:07:18 +0000 Subject: [PATCH] Added Sieve of Eratosthenes algorithm for finding primes --- FindingPrimes.java | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 FindingPrimes.java diff --git a/FindingPrimes.java b/FindingPrimes.java new file mode 100644 index 00000000..ecf3f7b4 --- /dev/null +++ b/FindingPrimes.java @@ -0,0 +1,30 @@ +/* + * The Sieve of Eratosthenes is an algorithm used to find prime numbers, up to a given value. + * Illustration: https://upload.wikimedia.org/wikipedia/commons/b/b9/Sieve_of_Eratosthenes_animation.gif +*/ +public class FindingPrimes{ + public static void main(String args[]){ + SOE(20); //Example: Finds all the primes up to 20 + } + + 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 + } + } +} \ No newline at end of file