回文素数

来源:互联网 发布:模拟考试软件下载 编辑:程序博客网 时间:2024/06/08 09:20

一个数同时为素数和回文数,显示前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;} 

}
这里写图片描述

0 0
原创粉丝点击