Two Powers of Two

Published by Mubashir Hassan in

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.
Watch a quick demo on how Edabit works.