Skip to content

Commit cd73823

Browse files
authored
different methods to find primes
its a famous method just thought it should exist in this repo :P
1 parent 7c8802e commit cd73823

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class SieveOfEratosthenes {
2+
void sieveOfEratosthenes(int n)
3+
{
4+
boolean prime[] = new boolean[n + 1];
5+
for (int i = 0; i <= n; i++)
6+
prime[i] = true;
7+
8+
for (int p = 2; p * p <= n; p++) {
9+
if (prime[p] == true) {
10+
for (int i = p * p; i <= n; i += p)
11+
prime[i] = false;
12+
}
13+
}
14+
15+
for (int i = 2; i <= n; i++) {
16+
if (prime[i] == true)
17+
System.out.print(i + " ");
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)