-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay25-Running-Time-and-Complexity
More file actions
43 lines (33 loc) · 1 KB
/
Day25-Running-Time-and-Complexity
File metadata and controls
43 lines (33 loc) · 1 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
Problem:
Objective
Today we're learning about running time! Check out the Tutorial tab for learning materials and an instructional video!
Task
A prime is a natural number greater than 1 that has no positive divisors other than 1 and itself.
Given a number,n , determine and print whether it's Prime or NotPrime.
Solution:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static boolean isPrime(int n) {
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int T = in.nextInt();
for (int i = 0; i < T; i++) {
int n = in.nextInt();
if (n >= 2 && isPrime(n))
System.out.println("Prime");
else System.out.println("Not prime");
}
in.close();
}
}