-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem22.java
66 lines (53 loc) · 1.46 KB
/
Problem22.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
65
66
package dimikOJ;
import java.util.Scanner;
public class Problem22 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
int T = input.nextInt();
while (T < 1) {
T = input.nextInt();
}
input.nextLine();
String[] lines = new String[T];
for (int i = 0; i < T; i++) {
String temp = input.nextLine();
String[] tempArray = temp.trim().replaceAll("\\s{2,}", " ").split("\\s");
int a = Integer.parseInt(tempArray[0]);
int b = Integer.parseInt(tempArray[1]);
while (a < 1 || a > 100000 || b < 1 || b > 100000) {
temp = input.nextLine();
tempArray = temp.trim().replaceAll("\\s{2,}", " ").split("\\s");
a = Integer.parseInt(tempArray[0]);
b = Integer.parseInt(tempArray[1]);
}
lines[i] = temp.trim().replaceAll("\\s{2,}", " ");
}
input.close();
for (int i = 0; i < lines.length; i++) {
String[] tempArray = lines[i].split("\\s");
int a = Integer.parseInt(tempArray[0]);
int b = Integer.parseInt(tempArray[1]);
int count = 0;
for (int j = a; j <= b; j++) {
if (isPrime(j)) {
count++;
}
}
System.out.println(count);
}
}
static boolean isPrime(int n) {
if (n <= 1)
return false;
else if (n == 2)
return true;
else if (n % 2 == 0)
return false;
for (int i = 3; i <= Math.sqrt(n); i += 2) {
if (n % i == 0)
return false;
}
return true;
}
}