Your challenge, should you choose to accept it, is, given an integer K >= 1
, find non-negative integers A
and B
such that at least one of the two conditions following hold:
K = 2^A + 2^B
K = 2^A - 2^B
If there does not exist such A
and B
, your program may behave in any fashion. (To clarify, A
and B
can be equal.)
Test cases
There are often multiple solutions to a number, but here are a few:
K => A, B
1 => 1, 0
15 => 4, 0 ; 16 - 1 = 15
16 => 5, 4 ; 32 - 16 = 16; also 3, 3: 8 + 8 = 16
40 => 5, 3 ; 2^5 + 2^3 = 40
264 => 8, 3
17179867136 => 34, 11 ; 17179869184 - 2048 = 17179867136
The last test case, 17179867136
, must run in under 10 seconds on any relatively modern machine. This is a code golf, so the shortest program in bytes wins. You may use a full program or a function.
16
, both5,4
and3,3
are valid. \$\endgroup\$A
,B
be negative? (e.g.-1, -1
for 1) \$\endgroup\$