How to check if a number is prime cyclops in Java

1 Answer

0 votes
// A number with odd number of digits and zero in the center is cyclops number

public class MyClass {
    private static boolean isCyclopsNumber(int n) {
        if (n == 0) {
            return true;
        }
     
        int m = n % 10;
        int count = 0;
        while (m != 0) {
            count++;
            n /= 10;
            m = n % 10;
        }
     
        n /= 10;
        m = n % 10;
        while (m != 0) {
            count--;
            n /= 10;
            m = n % 10;
        }
     
        return n == 0 && count == 0;
    }
    private static boolean isPrime(int n) {
        if (n < 2 || (n % 2 == 0 && n != 2)) {
            return false;
        }
        
        int count = (int)Math.floor(Math.sqrt(n));
        for (int i = 3; i <= count; i += 2) {
            if (n % i == 0) {
                return false;
            }
        }
        return true;
    }
    public static void main(String args[]) {
        
        int n = 209;
        System.out.println(isCyclopsNumber(n) && isPrime(n) ? "yes" : "no");
        
        n = 503;
        System.out.println(isCyclopsNumber(n) && isPrime(n) ? "yes" : "no");
    }
}
 
 
 
 
 
 
/*
run:
  
no
yes
  
*/

 



answered Mar 29, 2023 by avibootz
edited Mar 29, 2023 by avibootz

Related questions

1 answer 103 views
1 answer 124 views
1 answer 123 views
1 answer 98 views
1 answer 94 views
1 answer 95 views
1 answer 75 views
...