-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtime-complexity-primality.java
More file actions
66 lines (49 loc) · 1.55 KB
/
time-complexity-primality.java
File metadata and controls
66 lines (49 loc) · 1.55 KB
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
65
66
/*
A prime is a natural number greater than that has no positive divisors other than and itself. Given integers, determine the primality of each integer and print whether it is Prime or Not prime on a new line.
Note: If possible, try to come up with an primality algorithm, or see what sort of optimizations you can come up with for an algorithm. Be sure to check out the Editorial after submitting your code!
Input Format
The first line contains an integer, , denoting the number of integers to check for primality.
Each of the subsequent lines contains an integer, , you must test for primality.
Constraints
Output Format
For each integer, print whether is Prime or Not prime on a new line.
Sample Input
3
12
5
7
Sample Output
Not prime
Prime
Prime
*/
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int p = in.nextInt();
for(int a0 = 0; a0 < p; a0++){
int n = in.nextInt();
System.out.println(isPrime(n) ? "Prime" : "Not prime");
}
}
public static boolean isPrime(int n) {
if (n <= 1)
return false;
else if (n <= 3)
return true;
else if (n % 2 == 0 || n % 3 == 0)
return false;
int i = 5;
while (i * i <= n) {
if (n % i == 0 || n % (i + 2) == 0)
return false;
i = i + 6;
}
return true;
}
}