Java 输出100以内的可逆素数(质数) (方法4将素数先存储在字符串中,然后输出字符串)

    xiaoxiao2022-07-12  143

    public class SuShu { public static void main(String[] args) { String str=""; for(int i=10;i<=100;i++) { boolean tag = true; for(int j=2;j<Math.pow(i, 0.5)+1;j++) {//循环判断是不是素数 if(i%j==0) { tag=false;//不是素数,标记不是素数 break; //不是素数,直接结束循环 } } int s = 0; if(tag) {//是素数,判断它的逆数 s=i/10+i%10*10; for(int j=2;j<Math.pow(s, 0.5);j++) {//通过循环判断,逆数是不是素数 if(s%j==0) { tag=false; break; } } if(tag) {//逆数也是素数,将此数添加到字符串中 str=str+i+" "; } } } System.out.print(str); } }
    最新回复(0)