Loading...
Submission
Id When Author Problem Language CPU Memory Verdict
16863 2024-04-19 15:22:28 dnt Binary Prime Java 113 ms 42452 kb Runtime Error - 9
Test Cases
CPU Memory Verdict
1 101 ms 38176 kb Accepted
2 106 ms 38800 kb Accepted
3 103 ms 38312 kb Accepted
4 104 ms 38860 kb Accepted
5 102 ms 42452 kb Accepted
6 104 ms 38340 kb Accepted
7 104 ms 38660 kb Accepted
8 105 ms 41888 kb Accepted
9 113 ms 42300 kb Runtime Error
10 - - Skipped
11 - - Skipped
Source Code
program.java
Download
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4.  
  5. // Function to count the number of set bits in a binary representation
  6. static int countSetBits(int n) {
  7. int count = 0;
  8. while (n > 0) {
  9. count += n & 1;
  10. n >>= 1;
  11. }
  12. return count;
  13. }
  14.  
  15. // Function to check if a number is prime
  16. static boolean isPrime(int n) {
  17. if (n <= 1) return false;
  18. for (int i = 2; i * i <= n; i++) {
  19. if (n % i == 0) return false;
  20. }
  21. return true;
  22. }
  23.  
  24. public static void main(String[] args) {
  25. Scanner sc = new Scanner(System.in);
  26.  
  27. // Read the number of queries
  28. int T = sc.nextInt();
  29.  
  30. for (int t = 0; t < T; t++) {
  31. // Read the decimal number
  32. int N = sc.nextInt();
  33.  
  34. // Convert decimal number to binary and count set bits
  35. int setBits = countSetBits(N);
  36.  
  37. // Check if the sum of set bits is prime
  38. if (isPrime(setBits)) {
  39. System.out.println("Binary prime");
  40. } else {
  41. System.out.println("-1");
  42. }
  43. }
  44. }
  45. }
  46.  
  47.