How can I find the nth weird touch?

My problem: Given the number n as input, we return the value of the nth weird prime. Example -29 is an odd prime because it is a prime number and its sum of digits (reduced to one digit) = 2 + 9 = 11 = 1+ 1 = 2, which is a prime number. nthStrange (2) = 3 nthStrange (8) = 41

This is what I have done so far -

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;
    }
}

My problem is in the second while while. This is not working fine and I cannot find the problem.

+4
source share
1 answer

As a rule, it is a good idea in programming to separate each logical part into a separate method (in more complex programs, you divide it into classes, components, etc.).

, ( ), ( , , , "sumDigit" ).

:

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
0

Source: https://habr.com/ru/post/1534159/


All Articles