-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSeiveOfEratosthenes.java
46 lines (36 loc) · 1.44 KB
/
SeiveOfEratosthenes.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.anudev.ds.dynamicprogramming;
import com.anudev.ds.arrays.ArraysUtility;
/*
* Finding all the prime numbers
* we wil create an array of that number. When we reach a number we will start
* marking down the multiple of that number starting from the square of that
* number. If the square of that number becomes greater than the maximum number
* n we will stop and all the numbers which are left are the prime number.
*/
public class SeiveOfEratosthenes {
private final int primeTillNumber = 100;
private int[] tempArray;
public void printPrimeNumbers() {
seiveOfEratosthenesAlgo();
ArraysUtility.printIntArray(tempArray);
}
private void seiveOfEratosthenesAlgo() {
tempArray = new int[primeTillNumber - 1];
// insert value in temp array
for (int i = 2; i <= primeTillNumber; i++) {
tempArray[i - 2] = i;
}
int maximumSqrtValueOfN = (int) Math.sqrt(primeTillNumber);
// we will run till the square root of max value
for (int i = 0; i <= maximumSqrtValueOfN - 1; i++) {
int value = tempArray[i];
// checking if the value is not zero
// starting from square of the value with multiple of value making it 0
if (value != 0) {
for (int j = value * value; j <= primeTillNumber; j = j + value) {
tempArray[j - 2] = 0;
}
}
}
}
}