Yesterday while playing with my kid I noticed the number in his toy train:
So we have $$4281$$ that can be split into $$4-2-8-1$$ or $$2^2-2^1-2^3-2^0$$
So simple challenge: given a non-negative number as input, return consistent truthy and falsey values that represent whether or not the string representation of the number (in base 10 and without leading zeroes) can be somehow split into numbers that are powers of 2.
Examples:
4281 truthy (4-2-8-1)
164 truthy (16-4 or 1-64)
8192 truthy (the number itself is a power of 2)
81024 truthy (8-1024 or 8-1-02-4)
101 truthy (1-01)
0 falsey (0 cannot be represented as 2^x for any x)
1 truthy
3 falsey
234789 falsey
256323 falsey (we have 256 and 32 but then 3)
8132 truthy (8-1-32)
Tests for very large numbers (not really necessary to be handled by your code):
81024256641116 truthy (8-1024-256-64-1-1-16)
64512819237913 falsey
This is code-golf, so may the shortest code for each language win!
int
type (4 bytes), but actually I do not mind if your code does not support very large numbers. Just state in your answer the limitations of your code. \$\endgroup\$101
(falsy because of the 0) ... or should this still be true (1 - 01
)? \$\endgroup\$101
case with the current answers and they all returntrue
, because it can be splitted into1-01
that are both powers of 2, so I'll consider that case to be truthy. \$\endgroup\$log2(n)
doesn't contain decimal digits after the comma. 2) Check ifn AND (n-1) == 0
. 3) Create a list of square-nrs and check ifn
is in that list. \$\endgroup\$