枚举8项素数和环

package test;
import java.util.*;
public class SuShu{
public static void main(String arg[])
{
        int t,k,s,x;
        int[] g=new int[10];//g数组表示8位数a中每位数的数字.
        int[] f=new int[10];//f数组统计8位数a中各个数字的频数。
        int[] b=new int[19];//b数组标记整数x是否为素数。
        long a,y;   
        for(k=1;k<=15;k++) 
            b[k]=0;   
        g[9]=1;
        s=0;    
        b[3]=b[5]=b[7]=b[11]=b[13]=1;        // 5个奇素数标记      
        System.out.println("8项素数和环:");     
        for(a=12345678;a<=18765432;a+=9)     // 步长为9枚举8位数       
        {
            t=0;
            y=a;     
            for(k=0;k<=9;k++) 
                f[k]=0;      
            for(k=1;k<=8;k++)  {
                x=(int)y%10;
                f[x]++;       //  分离a的8个数字,用f数组统计x的个数        
                g[9-k]=x;            //  用g数组记录a的第k位数字     
                y=y/10;                             
            }       
        for(k=1;k<=8;k++)                       
            if(f[k]!=1 || b[g[k]+g[k+1]]!=1) t=1;       
            if(t==1) continue;     //  有相同数字或相邻和非素,返回         
            s++;                                 
            System.out.print(s+": 1,");    //  输出8项素数和环         
            for(k=2;k<=8;k++)       
                System.out.print(g[k]+",");      
            System.out.println("");      
    } 

}
}

原文地址:https://www.cnblogs.com/ljs-666/p/8001524.html