Skip to content

Commit b2de5c7

Browse files
authored
Add description for Sieve of Eratosthenes algorithm (Fixes: TheAlgorithms#2724) (TheAlgorithms#2725)
1 parent 1899d2a commit b2de5c7

File tree

1 file changed

+53
-21
lines changed

1 file changed

+53
-21
lines changed

Others/SieveOfEratosthenes.java

+53-21
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,76 @@
11
package Others;
22

3-
/** @author Varun Upadhyay (https://github.com/varunu28) */
3+
import java.util.Arrays;
4+
5+
/**
6+
* Sieve of Eratosthenes is an ancient algorithm for finding all prime numbers up to any given limit.
7+
* It does so by iteratively marking as composite (i.e., not prime) the multiples of each prime,
8+
* starting with the first prime number, 2.
9+
* The multiples of a given prime are generated as a sequence of numbers starting from that prime,
10+
* with constant difference between them that is equal to that prime.
11+
* This is the sieve's key distinction from using trial division to sequentially test each
12+
* candidate number for divisibility by each prime.
13+
* Once all the multiples of each discovered prime have been marked as composites, the remaining
14+
* unmarked numbers are primes.
15+
* <p>
16+
* Poetry about Sieve of Eratosthenes:
17+
* <p><i>Sift the Two's and Sift the Three's:</i></p>
18+
* <p><i>The Sieve of Eratosthenes.</i></p>
19+
* <p><i>When the multiples sublime,</i></p>
20+
* <p><i>The numbers that remain are Prime.</i></p>
21+
*
22+
* @see <a href="https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes">Wiki</a>
23+
*/
424
public class SieveOfEratosthenes {
525

626
/**
7-
* This method implements the Sieve of Eratosthenes Algorithm
8-
*
9-
* @param n The number till which we have to check for prime Prints all the prime numbers till n
27+
* @param n The number till which we have to check for prime Prints all the prime numbers till n.
28+
* Should be more than 1.
29+
* @return array of all prime numbers between 0 to n
1030
*/
11-
public static void findPrimesTillN(int n) {
12-
int[] arr = new int[n + 1];
13-
14-
for (int i = 0; i <= n; i++) {
15-
arr[i] = 1;
16-
}
31+
public static int[] findPrimesTill(int n) {
32+
// Create array where index is number and value is flag - is that number a prime or not.
33+
// size of array is n + 1 cause in Java array indexes starts with 0
34+
Type[] numbers = new Type[n + 1];
1735

18-
arr[0] = arr[1] = 0;
36+
// Start with assumption that all numbers except 0 and 1 are primes.
37+
Arrays.fill(numbers, Type.PRIME);
38+
numbers[0] = numbers[1] = Type.NOT_PRIME;
1939

20-
for (int i = 2; i <= Math.sqrt(n); i++) {
21-
if (arr[i] == 1) {
40+
double cap = Math.sqrt(n);
41+
// Main algorithm: mark all numbers which are multiples of some other values as not prime
42+
for (int i = 2; i <= cap; i++) {
43+
if (numbers[i] == Type.PRIME) {
2244
for (int j = 2; i * j <= n; j++) {
23-
arr[i * j] = 0;
45+
numbers[i * j] = Type.NOT_PRIME;
2446
}
2547
}
2648
}
2749

50+
//Write all primes to result array
51+
int primesCount = (int) Arrays.stream(numbers)
52+
.filter(element -> element == Type.PRIME)
53+
.count();
54+
int[] primes = new int[primesCount];
55+
56+
int primeIndex = 0;
2857
for (int i = 0; i < n + 1; i++) {
29-
if (arr[i] == 1) {
30-
System.out.print(i + " ");
58+
if(numbers[i] == Type.PRIME) {
59+
primes[primeIndex++] = i;
3160
}
3261
}
3362

34-
System.out.println();
63+
return primes;
64+
}
65+
66+
private enum Type {
67+
PRIME, NOT_PRIME
3568
}
3669

37-
// Driver Program
3870
public static void main(String[] args) {
3971
int n = 100;
40-
41-
// 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
42-
findPrimesTillN(n);
72+
System.out.println("Searching for all primes from zero to " + n);
73+
int[] primes = findPrimesTill(n);
74+
System.out.println("Found: " + Arrays.toString(primes));
4375
}
4476
}

0 commit comments

Comments
 (0)