-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeneratePrime.java
More file actions
31 lines (28 loc) · 820 Bytes
/
GeneratePrime.java
File metadata and controls
31 lines (28 loc) · 820 Bytes
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
/**
* Created by MalhotR1 on 2/15/2017.
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class GeneratePrime {
public static void main(String[] args) throws IOException {
System.out.println("1 neither prime nor composite.");
for (int t = 2; t < 100; t++) {
if (t == 2 || t == 3) {
System.out.println(t);
continue;
} else if (checkPrime(t)) {
System.out.println(t);
}
}
}
private static boolean checkPrime(int n) {
if (n % 2 == 0 || n % 3 == 0) return false;
int k = (int) Math.sqrt(n) + 1;
for (int i = 2; i < k; i++) {
if (k % i == 0)
return false;
}
return true;
}
}