/
B21.java
53 lines (42 loc) · 1.38 KB
/
B21.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
public class B21 {
/*
21, Viết chương trình liệt kê các số nguyên có 5-7 chữ số thoả mãn:
a) Là số nguyên tố.
b) Là số thuận nghịch.
c) Tổng các chữ số của số đó là một số thuận nghịch
*/
public static boolean checkSNT(int n) {
if (n > 1) {
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) return false;
}
return true;
} else return false;
}
public static boolean testSoThuanNghich(int n) {
StringBuilder xau = new StringBuilder();
String str = "" + n;
xau.append(str);
String check = "" + xau.reverse();
if (str.equals(check)) return true;
else return false;
}
public static boolean nguyenTo(int n) {
while (n != 0) {
if (!checkSNT(n % 10)) return false;
n /= 10;
}
return true;
}
public static void main(String[] args) {
int i, count = 0;
System.out.println("cac so tu 5-7 chu so thoa man dieu kien la: ");
for (i = 22223; i < 7777777; i += 2) {
if (checkSNT(i) && nguyenTo(i) && testSoThuanNghich(i)) {
System.out.println(" " + i);
count++;
}
}
System.out.println("\n Co " + count + " so thoa man");
}
}