← Back to challenges

Two Powers of Two

PythonHardmathnumbersalgorithms

Instructions

Given a number n, return whether it can be expressed as the sum of two powers of two. That means the sum of these types of 'doubling' numbers 1, 2, 4, 8, 16, 32, etc ...

Examples

two_powers_of_two(9) ➞ True
# Can be expressed as 1 + 8 (2^0 + 2^3).

two_powers_of_two(32) ➞ True
# Can be expressed as 16 + 16 (2^4 + 2^4)

two_powers_of_two(68) ➞ True
# Can be expressed as 64 + 4 (2^6 + 2^2)

two_powers_of_two(14) ➞ False

Notes

  • The two powers of two don't have to be unique.
  • Inputs are whole numbers >= 0
  • The powers of two themselves don't have to be whole numbers, so 1 can be considered as valid: 0.5 + 0.5 or 2^-1 + 2^-1
  • You can find a similar challenge here.
python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.