Java语言高分悬赏:输出10000以内结尾为3,倒数第二位为偶数的所有质数
提供思路如下;
1、抽取一个判断质数的方法;
2、抽取一个判断满足规则的方法;
3、循环一万以内的数,判断满足条件 1 的数是否也满足 2,是就输出。
public class PrimeNumber {
public static void main(String[] args) {
//10000以内结尾为3,倒数第二位为偶数的所有质数
int i; //作为除数循环
for(int thousand = 0; thousand <= 9; thousand++) //千位
for(int hundred = 0; hundred <= 9; hundred++) //百位
for(int ten = 0; ten <= 8; ten+=2) { //十位
int num = thousand*1000+hundred*100+ten*10+3; //千百十位加上3
for(i = 2; i <= num; i++) //判断是否为质数
if(num % i == 0) break;
if(num == i) System.out.println(num+" ");
}
}
}
package com.test;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
int last = 9983;// 10000以内,倒数第一为为3,倒数第二位为偶数,到9983截止
int start = 23;// 倒数第一为为3,倒数第二位为偶数,从23开始
for (int i = start; i <= last; i++) {
if (!match1(i)) {
continue;
}
if(match2(i)) {
System.out.println("满足条件,该值为:"+i);
}
}
}
/**
* 判断是否是质数
*
* @param i
* @return
*/
private static boolean match2(int n) {
for (int i = 2; Math.sqrt(n) >= i; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
/**
* 判断是否满足:最后一位是3 ,倒数第二位是偶数
*
* @param i
* @return
*/
private static boolean match1(int i) {
String str = String.valueOf(i);
if (str.lastIndexOf("3") != str.length() - 1) {
return false;
}
char lastSecondC = str.charAt(str.length() - 2);
Integer lastSecond = Integer.valueOf(lastSecondC);
if (lastSecond % 2 != 0) {
return false;
}
return true;
}
}
//输出结果:
满足条件,该值为:23
满足条件,该值为:43
满足条件,该值为:83
满足条件,该值为:103
满足条件,该值为:163
满足条件,该值为:223
满足条件,该值为:263
满足条件,该值为:283
满足条件,该值为:383
满足条件,该值为:443
满足条件,该值为:463
满足条件,该值为:503
满足条件,该值为:523
满足条件,该值为:563
满足条件,该值为:643
满足条件,该值为:683
满足条件,该值为:743
满足条件,该值为:823
满足条件,该值为:863
满足条件,该值为:883
满足条件,该值为:983
满足条件,该值为:1063
满足条件,该值为:1103
满足条件,该值为:1123
满足条件,该值为:1163
满足条件,该值为:1223
满足条件,该值为:1283
满足条件,该值为:1303
满足条件,该值为:1423
满足条件,该值为:1483
满足条件,该值为:1523
满足条件,该值为:1543
满足条件,该值为:1583
满足条件,该值为:1663
满足条件,该值为:1723
满足条件,该值为:1783
满足条件,该值为:1823
满足条件,该值为:2003
满足条件,该值为:2063
满足条件,该值为:2083
满足条件,该值为:2143
满足条件,该值为:2203
满足条件,该值为:2243
满足条件,该值为:2383
满足条件,该值为:2423
满足条件,该值为:2503
满足条件,该值为:2543
满足条件,该值为:2663
满足条件,该值为:2683
满足条件,该值为:2803
满足条件,该值为:2843
满足条件,该值为:2903
满足条件,该值为:2963
满足条件,该值为:3023
满足条件,该值为:3083
满足条件,该值为:3163
满足条件,该值为:3203
满足条件,该值为:3323
满足条件,该值为:3343
满足条件,该值为:3463
满足条件,该值为:3583
满足条件,该值为:3623
满足条件,该值为:3643
满足条件,该值为:3803
满足条件,该值为:3823
满足条件,该值为:3863
满足条件,该值为:3923
满足条件,该值为:3943
满足条件,该值为:4003
满足条件,该值为:4243
满足条件,该值为:4283
满足条件,该值为:4363
满足条件,该值为:4423
满足条件,该值为:4463
满足条件,该值为:4483
满足条件,该值为:4523
满足条件,该值为:4583
满足条件,该值为:4603
满足条件,该值为:4643
满足条件,该值为:4663
满足条件,该值为:4703
满足条件,该值为:4723
满足条件,该值为:4783
满足条件,该值为:4903
满足条件,该值为:4943
满足条件,该值为:5003
满足条件,该值为:5023
满足条件,该值为:5303
满足条件,该值为:5323
满足条件,该值为:5443
满足条件,该值为:5483
满足条件,该值为:5503
满足条件,该值为:5563
满足条件,该值为:5623
满足条件,该值为:5683
满足条件,该值为:5743
满足条件,该值为:5783
满足条件,该值为:5843
满足条件,该值为:5903
满足条件,该值为:5923
满足条件,该值为:6043
满足条件,该值为:6143
满足条件,该值为:6163
满足条件,该值为:6203
满足条件,该值为:6263
满足条件,该值为:6323
满足条件,该值为:6343
满足条件,该值为:6563
满足条件,该值为:6703
满足条件,该值为:6763
满足条件,该值为:6803
满足条件,该值为:6823
满足条件,该值为:6863
满足条件,该值为:6883
满足条件,该值为:6983
满足条件,该值为:7043
满足条件,该值为:7103
满足条件,该值为:7243
满足条件,该值为:7283
满足条件,该值为:7523
满足条件,该值为:7583
满足条件,该值为:7603
满足条件,该值为:7643
满足条件,该值为:7703
满足条件,该值为:7723
满足条件,该值为:7823
满足条件,该值为:7883
满足条件,该值为:7963
满足条件,该值为:8123
满足条件,该值为:8243
满足条件,该值为:8263
满足条件,该值为:8363
满足条件,该值为:8423
满足条件,该值为:8443
满足条件,该值为:8543
满足条件,该值为:8563
满足条件,该值为:8623
满足条件,该值为:8663
满足条件,该值为:8783
满足条件,该值为:8803
满足条件,该值为:8863
满足条件,该值为:8923
满足条件,该值为:8963
满足条件,该值为:9043
满足条件,该值为:9103
满足条件,该值为:9203
满足条件,该值为:9283
满足条件,该值为:9323
满足条件,该值为:9343
满足条件,该值为:9403
满足条件,该值为:9463
满足条件,该值为:9623
满足条件,该值为:9643
满足条件,该值为:9743
满足条件,该值为:9803
满足条件,该值为:9883
满足条件,该值为:9923
public static void main(String[] args) {
for(int i = 1;i<=1000;i++) {
int sum = 0;
for(int j = 1;j<i;j++) {
if(i%j == 0) {
sum += j;
}
}
if(sum == i) {
System.out.println(i);
}
}
}