我怎样才能找到第n个奇怪的素数?

时间:2014-03-30 20:36:10

标签: java primes

我的问题是: 给定一个数字n作为输入,返回第n个奇数素数的值。例子-29是一个奇数素数,因为它是一个素数及其数字之和(减少到一个数字)= 2 + 9 = 11 = 1+ 1 = 2这是一个素数。 nthStrange(2)= 3 nthStrange(8)= 41

这是我到目前为止所做的 -

public class NthStrangePrime{

    static int testcase1=1;
    static int testcase2=6;

    public static void main(String args[]){
        NthStrangePrime testInstance=new NthStrangePrime();
        int result=testInstance.nThStrangePrime(testcase2);
        System.out.println(result);
    }

    public int nThStrangePrime(int n){
        int count=0;
        int sum=0;
        int num=2;
        int s=0;
        while(count!=n){
            while(num>0){
                sum=sum+num%10;
                num=num/10;
            }
            while(sum>0){
                s=s+sum%10;
                sum=sum/10;
                //System.out.println(s);
            }
            int j=2;
            while(j<=s-1){
                if(s%j==0){
                    break;
                }
                j++;
            }
            if(j==s){
                System.out.println("This is a Strange Prime :"+s);
                count++;
            }
            num=num+1;
        }
        return s;
    }
}

我的问题是在第二次循环时。它工作不正常,我无法找出问题。

1 个答案:

答案 0 :(得分:0)

在编程中,将每个逻辑部分分成单独的方法(在更复杂的程序中,将它分成类,组件等)通常是个好主意。

然后你可以轻松地分别测试每个逻辑部分,它更可重复使用(使用已有的方法),它更加用户友好(你只是看它,你看,啊,我在这里做&#34; sumDigit& #34;。)

这是有效的代码:

public class NthStrangePrime {
    public static void main(String args[]) {
        System.out.println("29 is : " + isStrangePrime(29));
        System.out.println("13 is : " + isStrangePrime(13));
        System.out.println("14 is : " + isStrangePrime(14));
        System.out.println("15 is : " + isStrangePrime(15));
        System.out.println("16 is : " + isStrangePrime(16));
        System.out.println("17 is : " + isStrangePrime(17));

        System.out.println("13th strange number is : " + nThStrangePrime(13));
    }

    public static int nThStrangePrime(int n){
        int strangePrimes = 0;
        int number = 2;
        while (strangePrimes != n){
            if (isStrangePrime(number)){
                strangePrimes++;
                System.out.println("This is a Strange Prime :"+number);
            }
            number++;
        }
        return number-1;
    }

    public static boolean isStrangePrime(int n) {
        while ((n > 9) && (isItPrime(n))){
            n = sumDigit(n);
        }            
        if ((n <= 9) && (isItPrime(n))){
            return true;
        } else {
            return false;
        }
    }

    public static int sumDigit(int n) {
        int sum = 0;
        while (n > 0) {
            sum += n % 10;
            n /= 10;
        }
        return sum;
    }

    public static boolean isItPrime(int n) {
        if (n <= 1){
            return false;
        }
        if (n == 2) {
            return true;
        }

        boolean isPrimeNumber = true;
        for (int i = 2; i < n; i++) {
            if (n % i == 0) {
                isPrimeNumber = false;
                break; // exit the inner for loop
            }
        }

        return isPrimeNumber;
    }
}

此代码的输出:

29 is : true
13 is : false
14 is : false
15 is : false
16 is : false
17 is : false
This is a Strange Prime :2
This is a Strange Prime :3
This is a Strange Prime :5
This is a Strange Prime :7
This is a Strange Prime :11
This is a Strange Prime :23
This is a Strange Prime :29
This is a Strange Prime :41
This is a Strange Prime :43
This is a Strange Prime :47
This is a Strange Prime :61
This is a Strange Prime :83
This is a Strange Prime :101
13th strange number is : 101