Java Program - Sieve of Eratosthenes
Mathematics for Programming Numbers (Article) Numbers (Program)
24
Given Input:
Expected Output:
Program:
import java.util.Arrays;
public class SieveOfEratosthenes {
public static void main(String[] args) {
int n = 50;
boolean[] prime = new boolean[n + 1];
Arrays.fill(prime, true);
prime[0] = prime[1] = false;
for (int i = 2; i * i <= n; i++) {
if (prime[i]) {
for (int j = i * i; j <= n; j += i)
prime[j] = false;
}
}
for (int i = 2; i <= n; i++) {
if (prime[i])
System.out.print(i + " ");
}
}
}
Output:
This Particular section is dedicated to Programs only. If you want learn more about Mathematics for Programming. Then you can visit below links to get more depth on this subject.
Program:
import java.util.Arrays; public class SieveOfEratosthenes { public static void main(String[] args) { int n = 50; boolean[] prime = new boolean[n + 1]; Arrays.fill(prime, true); prime[0] = prime[1] = false; for (int i = 2; i * i <= n; i++) { if (prime[i]) { for (int j = i * i; j <= n; j += i) prime[j] = false; } } for (int i = 2; i <= n; i++) { if (prime[i]) System.out.print(i + " "); } } }
Output:
This Particular section is dedicated to Programs only. If you want learn more about Mathematics for Programming. Then you can visit below links to get more depth on this subject.