-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathMain.java
48 lines (43 loc) · 1.05 KB
/
Main.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
package pat.pat1007;
import java.util.Scanner;
/**
* 1007 素数对猜想 (20分)
* <p>
* 题目描述: https://pintia.cn/problem-sets/994805260223102976/problems/994805317546655744
*
* @author yangyi
*/
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int temp = Integer.MIN_VALUE;
int count = 0;
for (int i = 1; i <= n; i++) {
if (isPrime(i)) {
if (i - temp == 2) {
count++;
}
temp = i;
}
}
System.out.println(count);
}
/**
* 只有1和它本身可以整除的数为素数
*/
static boolean isPrime(int a) {
if (a == 1) {
return false;
}
if (a % 2 == 0 && a != 2) {
return false;
}
for (int i = 3; i <= Math.sqrt(a); i += 2) {
if (a % i == 0) {
return false;
}
}
return true;
}
}