JAVA——孿生素數


若兩個素數之差為2,則稱其為孿生素數

 

package 孿生素數;

public class 孿生素數 {
    public static void main(String[] args)
    {
        int i,j;
        int flag, n = 0;
        int a[] = new int[100];
        
        for(i = 2; i <= 100; i++)
        {
            flag = 1;
            
            for(j = 2; j < i; j++)
            {
                if(i%j == 0)
                {
                    flag = 0;
                    break;
                }
            }
            
            if(flag == 1)
            {
                a[n] = i;
                n++;
            }
            
        }
        
        System.out.println("100以內的孿生素數如下:");
        
        for(i = 0; i < n-1; i++)
        {
            if(a[i+1]-a[i] == 2)
            {
                System.out.print(a[i]+" ");
                System.out.print(a[i+1]+" ");
                System.out.println("\n");
            }
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM