|
| 1 | +import java.io.*; |
| 2 | +import java.util.*; |
| 3 | +interface PerformOperation { |
| 4 | + boolean check(int a); |
| 5 | +} |
| 6 | +class MyMath { |
| 7 | + public static boolean checker(PerformOperation p, int num) { |
| 8 | + return p.check(num); |
| 9 | + } |
| 10 | + |
| 11 | + /** |
| 12 | + * Returns a lambda expression that checks if a number is odd. |
| 13 | + * |
| 14 | + * @return A PerformOperation that checks if a number is odd. |
| 15 | + */ |
| 16 | + |
| 17 | + public static PerformOperation isOdd() { |
| 18 | + return n -> (n & 1) == 1; // Use bitwise AND to check if the number is odd |
| 19 | + } |
| 20 | + |
| 21 | + /** |
| 22 | + * Returns a lambda expression that checks if a number is prime. |
| 23 | + * |
| 24 | + * @return A PerformOperation that checks if a number is prime. |
| 25 | + */ |
| 26 | + |
| 27 | + public static PerformOperation isPrime() { |
| 28 | + return n -> { |
| 29 | + if (n < 2) { |
| 30 | + return false; // Numbers less than 2 are not prime |
| 31 | + } else if (n == 2) { |
| 32 | + return true; // 2 is the smallest prime number |
| 33 | + } else if (n % 2 == 0) { |
| 34 | + return false; // Even numbers greater than 2 are not prime |
| 35 | + } |
| 36 | + int sqrt = (int) Math.sqrt(n); // Check divisibility up to the square root of the number |
| 37 | + for (int i = 3; i <= sqrt; i += 2) { |
| 38 | + if (n % i == 0) { |
| 39 | + return false; // If divisible by any number, it's not prime |
| 40 | + } |
| 41 | + } |
| 42 | + return true; // If no divisors were found, it's prime |
| 43 | + }; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Returns a lambda expression that checks if a number is a palindrome. |
| 48 | + * |
| 49 | + * @return A PerformOperation that checks if a number is a palindrome. |
| 50 | + */ |
| 51 | + |
| 52 | + public static PerformOperation isPalindrome() { |
| 53 | + return n -> { |
| 54 | + String original = Integer.toString(n); // Convert the number to a string |
| 55 | + String reversed = new StringBuilder(Integer.toString(n)).reverse().toString(); // Reverse the string |
| 56 | + return original.equals(reversed); // Check if the original and reversed strings are equal |
| 57 | + }; |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +public class Solution { |
| 62 | + |
| 63 | + public static void main(String[] args) throws IOException { |
| 64 | + MyMath ob = new MyMath(); |
| 65 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 66 | + int T = Integer.parseInt(br.readLine()); |
| 67 | + PerformOperation op; |
| 68 | + boolean ret = false; |
| 69 | + String ans = null; |
| 70 | + while (T--> 0) { |
| 71 | + String s = br.readLine().trim(); |
| 72 | + StringTokenizer st = new StringTokenizer(s); |
| 73 | + int ch = Integer.parseInt(st.nextToken()); |
| 74 | + int num = Integer.parseInt(st.nextToken()); |
| 75 | + if (ch == 1) { |
| 76 | + op = ob.isOdd(); |
| 77 | + ret = ob.checker(op, num); |
| 78 | + ans = (ret) ? "ODD" : "EVEN"; |
| 79 | + } else if (ch == 2) { |
| 80 | + op = ob.isPrime(); |
| 81 | + ret = ob.checker(op, num); |
| 82 | + ans = (ret) ? "PRIME" : "COMPOSITE"; |
| 83 | + } else if (ch == 3) { |
| 84 | + op = ob.isPalindrome(); |
| 85 | + ret = ob.checker(op, num); |
| 86 | + ans = (ret) ? "PALINDROME" : "NOT PALINDROME"; |
| 87 | + |
| 88 | + } |
| 89 | + System.out.println(ans); |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments