java编程 有没有会这题的 谢谢帮忙
1个回答
展开全部
Java程序如下:
public class Test {
public static void main(String[] args) {
for(int i=3; i<1000; i+=2) {
if(isPrime(i) && isPrime(i+2)) {
System.out.printf("(%d, %d)\n", i, i+2);
}
}
}
static boolean isPrime(int x) {
int k = (int) Math.sqrt(x);
for(int i=2; i<=k; i++) {
if(x % i == 0) {
return false;
}
}
return true;
}
}
运行结果:
(3, 5)
(5, 7)
(11, 13)
(17, 19)
(29, 31)
(41, 43)
(59, 61)
(71, 73)
(101, 103)
(107, 109)
(137, 139)
(149, 151)
(179, 181)
(191, 193)
(197, 199)
(227, 229)
(239, 241)
(269, 271)
(281, 283)
(311, 313)
(347, 349)
(419, 421)
(431, 433)
(461, 463)
(521, 523)
(569, 571)
(599, 601)
(617, 619)
(641, 643)
(659, 661)
(809, 811)
(821, 823)
(827, 829)
(857, 859)
(881, 883)
public class Test {
public static void main(String[] args) {
for(int i=3; i<1000; i+=2) {
if(isPrime(i) && isPrime(i+2)) {
System.out.printf("(%d, %d)\n", i, i+2);
}
}
}
static boolean isPrime(int x) {
int k = (int) Math.sqrt(x);
for(int i=2; i<=k; i++) {
if(x % i == 0) {
return false;
}
}
return true;
}
}
运行结果:
(3, 5)
(5, 7)
(11, 13)
(17, 19)
(29, 31)
(41, 43)
(59, 61)
(71, 73)
(101, 103)
(107, 109)
(137, 139)
(149, 151)
(179, 181)
(191, 193)
(197, 199)
(227, 229)
(239, 241)
(269, 271)
(281, 283)
(311, 313)
(347, 349)
(419, 421)
(431, 433)
(461, 463)
(521, 523)
(569, 571)
(599, 601)
(617, 619)
(641, 643)
(659, 661)
(809, 811)
(821, 823)
(827, 829)
(857, 859)
(881, 883)
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询