From b2de5c7f1eda5ab1e48749dfba41acb001cc328a Mon Sep 17 00:00:00 2001 From: Artem Boiarshinov <54187376+Boiarshinov@users.noreply.github.com> Date: Wed, 27 Oct 2021 22:01:53 +0300 Subject: [PATCH] Add description for Sieve of Eratosthenes algorithm (Fixes: #2724) (#2725) --- Others/SieveOfEratosthenes.java | 74 +++++++++++++++++++++++---------- 1 file changed, 53 insertions(+), 21 deletions(-) diff --git a/Others/SieveOfEratosthenes.java b/Others/SieveOfEratosthenes.java index f3759d2f..a4293cc3 100644 --- a/Others/SieveOfEratosthenes.java +++ b/Others/SieveOfEratosthenes.java @@ -1,44 +1,76 @@ package Others; -/** @author Varun Upadhyay (https://github.com/varunu28) */ +import java.util.Arrays; + +/** + * Sieve of Eratosthenes is an ancient algorithm for finding all prime numbers up to any given limit. + * It does so by iteratively marking as composite (i.e., not prime) the multiples of each prime, + * starting with the first prime number, 2. + * The multiples of a given prime are generated as a sequence of numbers starting from that prime, + * with constant difference between them that is equal to that prime. + * This is the sieve's key distinction from using trial division to sequentially test each + * candidate number for divisibility by each prime. + * Once all the multiples of each discovered prime have been marked as composites, the remaining + * unmarked numbers are primes. + *

+ * Poetry about Sieve of Eratosthenes: + *

Sift the Two's and Sift the Three's:

+ *

The Sieve of Eratosthenes.

+ *

When the multiples sublime,

+ *

The numbers that remain are Prime.

+ * + * @see Wiki + */ public class SieveOfEratosthenes { /** - * This method implements the Sieve of Eratosthenes Algorithm - * - * @param n The number till which we have to check for prime Prints all the prime numbers till n + * @param n The number till which we have to check for prime Prints all the prime numbers till n. + * Should be more than 1. + * @return array of all prime numbers between 0 to n */ - public static void findPrimesTillN(int n) { - int[] arr = new int[n + 1]; + public static int[] findPrimesTill(int n) { + // Create array where index is number and value is flag - is that number a prime or not. + // size of array is n + 1 cause in Java array indexes starts with 0 + Type[] numbers = new Type[n + 1]; - for (int i = 0; i <= n; i++) { - arr[i] = 1; - } + // Start with assumption that all numbers except 0 and 1 are primes. + Arrays.fill(numbers, Type.PRIME); + numbers[0] = numbers[1] = Type.NOT_PRIME; - arr[0] = arr[1] = 0; - - for (int i = 2; i <= Math.sqrt(n); i++) { - if (arr[i] == 1) { + double cap = Math.sqrt(n); + // Main algorithm: mark all numbers which are multiples of some other values as not prime + for (int i = 2; i <= cap; i++) { + if (numbers[i] == Type.PRIME) { for (int j = 2; i * j <= n; j++) { - arr[i * j] = 0; + numbers[i * j] = Type.NOT_PRIME; } } } + //Write all primes to result array + int primesCount = (int) Arrays.stream(numbers) + .filter(element -> element == Type.PRIME) + .count(); + int[] primes = new int[primesCount]; + + int primeIndex = 0; for (int i = 0; i < n + 1; i++) { - if (arr[i] == 1) { - System.out.print(i + " "); + if(numbers[i] == Type.PRIME) { + primes[primeIndex++] = i; } } - System.out.println(); + return primes; + } + + private enum Type { + PRIME, NOT_PRIME } - // Driver Program public static void main(String[] args) { int n = 100; - - // Prints 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 - findPrimesTillN(n); + System.out.println("Searching for all primes from zero to " + n); + int[] primes = findPrimesTill(n); + System.out.println("Found: " + Arrays.toString(primes)); } }