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...