Skip to content

Commit

Permalink
different methods to find primes
Browse files Browse the repository at this point in the history
its a famous method just thought it should exist in this repo :P
  • Loading branch information
Shad0w0192 authored Oct 24, 2024
1 parent 7c8802e commit cd73823
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Finding-primes/different-sieves.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class SieveOfEratosthenes {
void sieveOfEratosthenes(int n)
{
boolean prime[] = new boolean[n + 1];
for (int i = 0; i <= n; i++)
prime[i] = true;

for (int p = 2; p * p <= n; p++) {
if (prime[p] == true) {
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}

for (int i = 2; i <= n; i++) {
if (prime[i] == true)
System.out.print(i + " ");
}
}
}

0 comments on commit cd73823

Please sign in to comment.