Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java boolean-algorithm to check wheter number of digits is even

I´m trying to write a recursive method that returns true, if the count of digits of n displayed as a binary number is even and false if it is odd. I don´t really get how I can count in a recursive method, if it returns a boolean.

I think part of the solution is, to check whether the number is a powers of 2:

static boolean isPowers2(long n) {
    if (n % 2 != 0) return false;
    if (n / 2 == 1) return true;
    return isPowers2(n / 2);
}

If the exponent is odd then the count of digits is even, if it is even then the count of digits is uneven. But I can not pass a value with my boolean function, right?

Some examples for what should be returned:

  // evenCount(0B0    ) is false
  // evenCount(0B1    ) is false
  // evenCount(0B10   ) is true
  // evenCount(0B11   ) is true
  // evenCount(0B100  ) is false
  // evenCount(0B111  ) is false
  // evenCount(0B1000 ) is true
  // evenCount(0B1010 ) is true
  // evenCount(0B10000) is false
  // evenCount(0B10110) istfalse

This is a question I failed on an algorithms test in university and I still can´t figure out how to solve it. I hope someone can give me a hint on how to solve this one...

like image 861
nicenoize Avatar asked Jan 31 '18 19:01

nicenoize


2 Answers

Checking if the input is a power of 2 is irrelevant. You should write a recursive method that removes one bit in each call.

A number has an even binary length if an only if it has an odd length if you remove one bit from it.

static boolean hasEvenLength(int n)
{
    if (n<2) { // single digit
        return false;
    }
    return !hasEvenLength(n/2);
}

This handles non-negative input. For negative numbers I can argue that you should always return true, since the most significant bit (the sign bit) is always set, so you can say the number has 32 binary digits.

Here's the output of the method for all numbers between 0 and 99:

0 0 false
1 1 false
2 10 true
3 11 true
4 100 false
5 101 false
6 110 false
7 111 false
8 1000 true
9 1001 true
10 1010 true
11 1011 true
12 1100 true
13 1101 true
14 1110 true
15 1111 true
16 10000 false
17 10001 false
18 10010 false
19 10011 false
20 10100 false
21 10101 false
22 10110 false
23 10111 false
24 11000 false
25 11001 false
26 11010 false
27 11011 false
28 11100 false
29 11101 false
30 11110 false
31 11111 false
32 100000 true
33 100001 true
34 100010 true
35 100011 true
36 100100 true
37 100101 true
38 100110 true
39 100111 true
40 101000 true
41 101001 true
42 101010 true
43 101011 true
44 101100 true
45 101101 true
46 101110 true
47 101111 true
48 110000 true
49 110001 true
50 110010 true
51 110011 true
52 110100 true
53 110101 true
54 110110 true
55 110111 true
56 111000 true
57 111001 true
58 111010 true
59 111011 true
60 111100 true
61 111101 true
62 111110 true
63 111111 true
64 1000000 false
65 1000001 false
66 1000010 false
67 1000011 false
68 1000100 false
69 1000101 false
70 1000110 false
71 1000111 false
72 1001000 false
73 1001001 false
74 1001010 false
75 1001011 false
76 1001100 false
77 1001101 false
78 1001110 false
79 1001111 false
80 1010000 false
81 1010001 false
82 1010010 false
83 1010011 false
84 1010100 false
85 1010101 false
86 1010110 false
87 1010111 false
88 1011000 false
89 1011001 false
90 1011010 false
91 1011011 false
92 1011100 false
93 1011101 false
94 1011110 false
95 1011111 false
96 1100000 false
97 1100001 false
98 1100010 false
99 1100011 false
like image 135
Eran Avatar answered Nov 12 '22 05:11

Eran


You don't need to count the digits, you just need to keep track of whether that count is odd or even.

boolean hasEvenDigitCount(long n) {
    if (n/2 == 0) { // handles both 1 and 0 as an even number of digits
        return false;
    }
    return !hasEvenDigitCount(n/2);
}
like image 36
Nelfeal Avatar answered Nov 12 '22 06:11

Nelfeal