天天看点

Java 输出100以内的可逆素数(质数) (方法2 将逆素数先存在一个数组里,再输出)

public class SuShu {
	public static void main(String[] args) {
	int [] a = new int [100] ;
		int count=0;
		for(int i=10;i<100;i++) {
		boolean tag=true;
			for(int j=2;j<Math.sqrt(i);j++) {
				if(i%j==0) {
					tag=false;
					break;
				}
			}//已经判断了素数
			if(tag) {
				int shiwei=i/10;
				int gewei=i%10;
				int c=gewei*10+shiwei; //取逆数,通过循环判断逆数是不是素数
				for(int j=2;j<Math.sqrt(c);j++) {
					if(c%j==0) {
						tag=false;
						break;
					}
				}//前面是判断素数中的逆素数
				if(tag) {//在逆素数的基础上,统计数据
					a[count]=i;//将素数存储于数组a中
					count++;
				}
			}
		}
		int [] b = new int [count];
			for(int i=0;i<count;i++) {
				b[i]=a[i];
			}
		System.out.print(Arrays.toString(b));
	}
}