迴文素數

一個數同時爲素數和迴文數,顯示前100個迴文素數
代碼:
package com.im;

public class Demo626 {

public static void main(String[] args) {
    // TODO Auto-generated method stub

// long Num = 1000000000;
int count = 1;

    for(int i=2; true; i++){


        if(isPrimeNum(i) && isPalindrome(i)){
            System.out.printf("%6d",i);

            if(count%10 == 0){
                System.out.println();
            }

            if(count == 100){
                break;
            }
            count++;
        }
    }

}

//判斷是否是素數
public static boolean isPrimeNum(long Num){

    for(int i=2; i<=Num/2; i++){
        if(Num % i == 0){
            return false;
        }
    }
    return true;
     //如果Num不存在 
}

//判斷是否是迴文數
public static boolean isPalindrome(long Num){
    if(Num == reverse(Num)){
        return true;
    }else{
        return false;
    }
}

//求出此數的倒置數
public static long reverse(long Num){

    long v = 0; //位數上的數字
    long m = 0;  //倒置數
    int count = digitOfNumber(Num);

    for(int i=1; i<=count; i++){
        v = Num % 10;
        m += v*Math.pow(10, count-i);
        Num /= 10;
    }

    return m;

}

//求出此數的位數
public static int digitOfNumber(long Num){

    int count = 0; //位數的計數器
    while(Num > 0){
        Num /= 10;
        count++;
    }
    return count;
} 

}
這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章