-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrime.java
64 lines (50 loc) · 1.61 KB
/
Prime.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Able to Check if a number is prime
// Get all prime numbers up a certain range using the Sieve of Erasthostenes
public class Prime {
public static void main(String[] args) {
System.out.println(" All prime numbers up to 0");
printPrimes(0);
System.out.println("\n");
System.out.println(" All prime numbers up to 2");
printPrimes(2);
System.out.println("\n");
System.out.println(" All prime numbers up to 10");
printPrimes(10);
System.out.println("\n");
System.out.println(" All prime numbers up to 50");
printPrimes(50);
System.out.println("\n");
System.out.println(" All prime numbers up to 100");
printPrimes(100);
System.out.println("\n");
}
//Ckeck if a number is Prime
public static boolean isPrime(int n) {
for(int divisor = 2; divisor * divisor < n; divisor++) {
if (n % divisor == 0) {
return false;
}
}
return true;
}
//Print all primes less or equal to n using Sieve of Erasthostenes
//Assume all numbers are prime till proven otherwise
public static void printPrimes (int n) {
boolean[] prime = new boolean[n+1];
for(int i = 2; i <= n; i++) {
prime[i] = true;
}
for (int divisor = 2; divisor * divisor <= n ; divisor ++ ) {
if (prime[divisor]) {
for( int j = 2*divisor; j <= n; j += divisor ) {
prime[j] = false;
}
}
}
for(int i = 2; i <= n; i++) {
if(prime[i]) {
System.out.println(" " + i);
}
}
}
}