mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-24 21:14:00 +08:00
docs: update the whole repository
* fix some bugs * delete duplicate files * format code
This commit is contained in:
@ -1,7 +1,7 @@
|
||||
package Others;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Varun Upadhyay (https://github.com/varunu28)
|
||||
*
|
||||
*/
|
||||
public class SieveOfEratosthenes {
|
||||
|
||||
@ -9,27 +9,27 @@ 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
|
||||
* Prints all the prime numbers till n
|
||||
**/
|
||||
|
||||
public static void findPrimesTillN(int n) {
|
||||
int[] arr = new int[n+1];
|
||||
int[] arr = new int[n + 1];
|
||||
|
||||
for (int i=0;i<=n;i++) {
|
||||
for (int i = 0; i <= n; i++) {
|
||||
arr[i] = 1;
|
||||
}
|
||||
|
||||
arr[0] = arr[1] = 0;
|
||||
|
||||
for (int i=2;i<=Math.sqrt(n);i++) {
|
||||
for (int i = 2; i <= Math.sqrt(n); i++) {
|
||||
if (arr[i] == 1) {
|
||||
for (int j=2;i*j <= n;j++) {
|
||||
arr[i*j] = 0;
|
||||
for (int j = 2; i * j <= n; j++) {
|
||||
arr[i * j] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (int i=0;i<n+1;i++) {
|
||||
for (int i = 0; i < n + 1; i++) {
|
||||
if (arr[i] == 1) {
|
||||
System.out.print(i + " ");
|
||||
}
|
||||
|
Reference in New Issue
Block a user