Can the number be split into powers of 2?
Clash Royale CLAN TAG#URR8PPP
up vote
6
down vote
favorite
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!
code-golf string number decision-problem
add a comment |Â
up vote
6
down vote
favorite
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!
code-golf string number decision-problem
Any limit to how many digits must be supported? Is it OK if it works in theory (assume much memory/time) for large numbers? The reason why I ask: A possible solution is to create all possible "splits" (don't remember the word).164 -> "1,6,4", "1, 64", "16,4", "164"
. The number of "splits" can get quite large for large numbers.
– Stewie Griffin
1 hour ago
@StewieGriffin initially I thought about limiting the input number to the range of a standardint
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.
– Charlie
1 hour ago
2
Suggested test case:101
(falsy because of the 0) ... or should this still be true (1 - 01
)?
– Shieru Asakoto
56 mins ago
1
@ShieruAsakoto I've been testing the101
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.
– Charlie
53 mins ago
1
Just leaving this here as tip for everyone. Here are three possible ways to check if a number is a power of 2: 1) Check iflog2(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.
– Kevin Cruijssen
37 mins ago
add a comment |Â
up vote
6
down vote
favorite
up vote
6
down vote
favorite
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!
code-golf string number decision-problem
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!
code-golf string number decision-problem
code-golf string number decision-problem
edited 11 mins ago


Luis Mendo
73.1k885285
73.1k885285
asked 1 hour ago


Charlie
6,8371979
6,8371979
Any limit to how many digits must be supported? Is it OK if it works in theory (assume much memory/time) for large numbers? The reason why I ask: A possible solution is to create all possible "splits" (don't remember the word).164 -> "1,6,4", "1, 64", "16,4", "164"
. The number of "splits" can get quite large for large numbers.
– Stewie Griffin
1 hour ago
@StewieGriffin initially I thought about limiting the input number to the range of a standardint
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.
– Charlie
1 hour ago
2
Suggested test case:101
(falsy because of the 0) ... or should this still be true (1 - 01
)?
– Shieru Asakoto
56 mins ago
1
@ShieruAsakoto I've been testing the101
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.
– Charlie
53 mins ago
1
Just leaving this here as tip for everyone. Here are three possible ways to check if a number is a power of 2: 1) Check iflog2(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.
– Kevin Cruijssen
37 mins ago
add a comment |Â
Any limit to how many digits must be supported? Is it OK if it works in theory (assume much memory/time) for large numbers? The reason why I ask: A possible solution is to create all possible "splits" (don't remember the word).164 -> "1,6,4", "1, 64", "16,4", "164"
. The number of "splits" can get quite large for large numbers.
– Stewie Griffin
1 hour ago
@StewieGriffin initially I thought about limiting the input number to the range of a standardint
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.
– Charlie
1 hour ago
2
Suggested test case:101
(falsy because of the 0) ... or should this still be true (1 - 01
)?
– Shieru Asakoto
56 mins ago
1
@ShieruAsakoto I've been testing the101
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.
– Charlie
53 mins ago
1
Just leaving this here as tip for everyone. Here are three possible ways to check if a number is a power of 2: 1) Check iflog2(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.
– Kevin Cruijssen
37 mins ago
Any limit to how many digits must be supported? Is it OK if it works in theory (assume much memory/time) for large numbers? The reason why I ask: A possible solution is to create all possible "splits" (don't remember the word).
164 -> "1,6,4", "1, 64", "16,4", "164"
. The number of "splits" can get quite large for large numbers.– Stewie Griffin
1 hour ago
Any limit to how many digits must be supported? Is it OK if it works in theory (assume much memory/time) for large numbers? The reason why I ask: A possible solution is to create all possible "splits" (don't remember the word).
164 -> "1,6,4", "1, 64", "16,4", "164"
. The number of "splits" can get quite large for large numbers.– Stewie Griffin
1 hour ago
@StewieGriffin initially I thought about limiting the input number to the range of a standard
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.– Charlie
1 hour ago
@StewieGriffin initially I thought about limiting the input number to the range of a standard
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.– Charlie
1 hour ago
2
2
Suggested test case:
101
(falsy because of the 0) ... or should this still be true (1 - 01
)?– Shieru Asakoto
56 mins ago
Suggested test case:
101
(falsy because of the 0) ... or should this still be true (1 - 01
)?– Shieru Asakoto
56 mins ago
1
1
@ShieruAsakoto I've been testing the
101
case with the current answers and they all return true
, because it can be splitted into 1-01
that are both powers of 2, so I'll consider that case to be truthy.– Charlie
53 mins ago
@ShieruAsakoto I've been testing the
101
case with the current answers and they all return true
, because it can be splitted into 1-01
that are both powers of 2, so I'll consider that case to be truthy.– Charlie
53 mins ago
1
1
Just leaving this here as tip for everyone. Here are three possible ways to check if a number is a power of 2: 1) Check if
log2(n)
doesn't contain decimal digits after the comma. 2) Check if n AND (n-1) == 0
. 3) Create a list of square-nrs and check if n
is in that list.– Kevin Cruijssen
37 mins ago
Just leaving this here as tip for everyone. Here are three possible ways to check if a number is a power of 2: 1) Check if
log2(n)
doesn't contain decimal digits after the comma. 2) Check if n AND (n-1) == 0
. 3) Create a list of square-nrs and check if n
is in that list.– Kevin Cruijssen
37 mins ago
add a comment |Â
6 Answers
6
active
oldest
votes
up vote
3
down vote
05AB1E, 9 bytes
ÃÂos.όPOĀ
Try it online or verify all test cases. (NOTE: The т
in the header is 100
to only get the first 100 square numbers, instead of the first input amount of square numbers. It works in the second case as well, but is pretty inefficient and might time-out on TIO.)
Explanation:
à# Create a list in the range [0,n], where n is the (implicit) input
# (or 100 in the TIO)
# i.e. 81024 → [0,1,2,3,...,81024]
o # Square each
# → [1,2,4,8,...,451..216 (nr with 24391 digits)]
s # Swap to take the input
.œ # Create each possible partition of this input
# i.e. 81024 → [["8","1","0","2","4"],["8","1","0","24"],...,["8102","4"],["81024"]]
Ã¥ # Check for each if it's in the list of squares
# → [[1,1,0,1,1],[1,1,0,0],...,[0,1],[0]]
P # Check for each inner list whether all are truthy
# → [0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0]
O # Take the sum
# → 2
Ā # Truthify (and output implicitly)
# → 1 (truthy)
1
Nice, my solution was.œ.²1%O0å
(9 bytes as well). Mine failed0
, however.
– Mr. Xcoder
33 mins ago
add a comment |Â
up vote
3
down vote
JavaScript (Node.js), 6964 58 bytes
f=(x,m=10,q=!(x%m&x%m-1|!x))=>x<m?q:q&&f(x/m|0)||f(x,10*m)
Try it online!
Input as number. The logic part is quite convoluted, so no idea how to untangle it and get rid of q
.
-11 bytes by golfing the power-of-2 check.
add a comment |Â
up vote
2
down vote
Python 2, 85 bytes
f=lambda n:bin(int(n)).count('1')==1or any(f(n[:i])*f(n[i:])for i in range(1,len(n)))
Try it online!
add a comment |Â
up vote
2
down vote
Python 2, 72 bytes
f=lambda n,m=10:n>=m/10and(n%m&~-(n%m)<1and(n/m<1or f(n/m))or f(n,m*10))
Try it online!
add a comment |Â
up vote
2
down vote
JavaScript (Node.js), 75 69 bytes
-6 bytes thanks @Arnauld. At most 32-bit support
f=x=>+x?[...x].some((_,i)=>(y=x.slice(0,++i))&~-y?0:f(x.slice(i))):!x
Try it online!
Input as a string.
@Arnauld Oh dang I forgot the bitwise trick again! Thanks!
– Shieru Asakoto
1 hour ago
add a comment |Â
up vote
1
down vote
Jelly, 9 bytes
ŒṖḌl2ĊƑ€Ẹ
Check out the test suite!
How?
ŒṖḌl2ĊƑ€Ẹ Full program. N = integer input.
ŒṖ All possible partitions of the digits of N.
Ḍ Undecimal (i.e. join to numbers).
l2 Log2. Note: returns -(-inf+nanj) for 0, so it doesn't fail.
ĊƑ€ For each, check if the logarithm equals its ceil.
Ẹ Any. Return 0 if there are no truthy elements, 1 otherwise.
add a comment |Â
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
05AB1E, 9 bytes
ÃÂos.όPOĀ
Try it online or verify all test cases. (NOTE: The т
in the header is 100
to only get the first 100 square numbers, instead of the first input amount of square numbers. It works in the second case as well, but is pretty inefficient and might time-out on TIO.)
Explanation:
à# Create a list in the range [0,n], where n is the (implicit) input
# (or 100 in the TIO)
# i.e. 81024 → [0,1,2,3,...,81024]
o # Square each
# → [1,2,4,8,...,451..216 (nr with 24391 digits)]
s # Swap to take the input
.œ # Create each possible partition of this input
# i.e. 81024 → [["8","1","0","2","4"],["8","1","0","24"],...,["8102","4"],["81024"]]
Ã¥ # Check for each if it's in the list of squares
# → [[1,1,0,1,1],[1,1,0,0],...,[0,1],[0]]
P # Check for each inner list whether all are truthy
# → [0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0]
O # Take the sum
# → 2
Ā # Truthify (and output implicitly)
# → 1 (truthy)
1
Nice, my solution was.œ.²1%O0å
(9 bytes as well). Mine failed0
, however.
– Mr. Xcoder
33 mins ago
add a comment |Â
up vote
3
down vote
05AB1E, 9 bytes
ÃÂos.όPOĀ
Try it online or verify all test cases. (NOTE: The т
in the header is 100
to only get the first 100 square numbers, instead of the first input amount of square numbers. It works in the second case as well, but is pretty inefficient and might time-out on TIO.)
Explanation:
à# Create a list in the range [0,n], where n is the (implicit) input
# (or 100 in the TIO)
# i.e. 81024 → [0,1,2,3,...,81024]
o # Square each
# → [1,2,4,8,...,451..216 (nr with 24391 digits)]
s # Swap to take the input
.œ # Create each possible partition of this input
# i.e. 81024 → [["8","1","0","2","4"],["8","1","0","24"],...,["8102","4"],["81024"]]
Ã¥ # Check for each if it's in the list of squares
# → [[1,1,0,1,1],[1,1,0,0],...,[0,1],[0]]
P # Check for each inner list whether all are truthy
# → [0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0]
O # Take the sum
# → 2
Ā # Truthify (and output implicitly)
# → 1 (truthy)
1
Nice, my solution was.œ.²1%O0å
(9 bytes as well). Mine failed0
, however.
– Mr. Xcoder
33 mins ago
add a comment |Â
up vote
3
down vote
up vote
3
down vote
05AB1E, 9 bytes
ÃÂos.όPOĀ
Try it online or verify all test cases. (NOTE: The т
in the header is 100
to only get the first 100 square numbers, instead of the first input amount of square numbers. It works in the second case as well, but is pretty inefficient and might time-out on TIO.)
Explanation:
à# Create a list in the range [0,n], where n is the (implicit) input
# (or 100 in the TIO)
# i.e. 81024 → [0,1,2,3,...,81024]
o # Square each
# → [1,2,4,8,...,451..216 (nr with 24391 digits)]
s # Swap to take the input
.œ # Create each possible partition of this input
# i.e. 81024 → [["8","1","0","2","4"],["8","1","0","24"],...,["8102","4"],["81024"]]
Ã¥ # Check for each if it's in the list of squares
# → [[1,1,0,1,1],[1,1,0,0],...,[0,1],[0]]
P # Check for each inner list whether all are truthy
# → [0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0]
O # Take the sum
# → 2
Ā # Truthify (and output implicitly)
# → 1 (truthy)
05AB1E, 9 bytes
ÃÂos.όPOĀ
Try it online or verify all test cases. (NOTE: The т
in the header is 100
to only get the first 100 square numbers, instead of the first input amount of square numbers. It works in the second case as well, but is pretty inefficient and might time-out on TIO.)
Explanation:
à# Create a list in the range [0,n], where n is the (implicit) input
# (or 100 in the TIO)
# i.e. 81024 → [0,1,2,3,...,81024]
o # Square each
# → [1,2,4,8,...,451..216 (nr with 24391 digits)]
s # Swap to take the input
.œ # Create each possible partition of this input
# i.e. 81024 → [["8","1","0","2","4"],["8","1","0","24"],...,["8102","4"],["81024"]]
Ã¥ # Check for each if it's in the list of squares
# → [[1,1,0,1,1],[1,1,0,0],...,[0,1],[0]]
P # Check for each inner list whether all are truthy
# → [0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0]
O # Take the sum
# → 2
Ā # Truthify (and output implicitly)
# → 1 (truthy)
answered 56 mins ago


Kevin Cruijssen
31.6k553171
31.6k553171
1
Nice, my solution was.œ.²1%O0å
(9 bytes as well). Mine failed0
, however.
– Mr. Xcoder
33 mins ago
add a comment |Â
1
Nice, my solution was.œ.²1%O0å
(9 bytes as well). Mine failed0
, however.
– Mr. Xcoder
33 mins ago
1
1
Nice, my solution was
.œ.²1%O0å
(9 bytes as well). Mine failed 0
, however.– Mr. Xcoder
33 mins ago
Nice, my solution was
.œ.²1%O0å
(9 bytes as well). Mine failed 0
, however.– Mr. Xcoder
33 mins ago
add a comment |Â
up vote
3
down vote
JavaScript (Node.js), 6964 58 bytes
f=(x,m=10,q=!(x%m&x%m-1|!x))=>x<m?q:q&&f(x/m|0)||f(x,10*m)
Try it online!
Input as number. The logic part is quite convoluted, so no idea how to untangle it and get rid of q
.
-11 bytes by golfing the power-of-2 check.
add a comment |Â
up vote
3
down vote
JavaScript (Node.js), 6964 58 bytes
f=(x,m=10,q=!(x%m&x%m-1|!x))=>x<m?q:q&&f(x/m|0)||f(x,10*m)
Try it online!
Input as number. The logic part is quite convoluted, so no idea how to untangle it and get rid of q
.
-11 bytes by golfing the power-of-2 check.
add a comment |Â
up vote
3
down vote
up vote
3
down vote
JavaScript (Node.js), 6964 58 bytes
f=(x,m=10,q=!(x%m&x%m-1|!x))=>x<m?q:q&&f(x/m|0)||f(x,10*m)
Try it online!
Input as number. The logic part is quite convoluted, so no idea how to untangle it and get rid of q
.
-11 bytes by golfing the power-of-2 check.
JavaScript (Node.js), 6964 58 bytes
f=(x,m=10,q=!(x%m&x%m-1|!x))=>x<m?q:q&&f(x/m|0)||f(x,10*m)
Try it online!
Input as number. The logic part is quite convoluted, so no idea how to untangle it and get rid of q
.
-11 bytes by golfing the power-of-2 check.
edited 44 mins ago
answered 1 hour ago


Bubbler
3,937541
3,937541
add a comment |Â
add a comment |Â
up vote
2
down vote
Python 2, 85 bytes
f=lambda n:bin(int(n)).count('1')==1or any(f(n[:i])*f(n[i:])for i in range(1,len(n)))
Try it online!
add a comment |Â
up vote
2
down vote
Python 2, 85 bytes
f=lambda n:bin(int(n)).count('1')==1or any(f(n[:i])*f(n[i:])for i in range(1,len(n)))
Try it online!
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Python 2, 85 bytes
f=lambda n:bin(int(n)).count('1')==1or any(f(n[:i])*f(n[i:])for i in range(1,len(n)))
Try it online!
Python 2, 85 bytes
f=lambda n:bin(int(n)).count('1')==1or any(f(n[:i])*f(n[i:])for i in range(1,len(n)))
Try it online!
answered 1 hour ago


TFeld
12.4k2834
12.4k2834
add a comment |Â
add a comment |Â
up vote
2
down vote
Python 2, 72 bytes
f=lambda n,m=10:n>=m/10and(n%m&~-(n%m)<1and(n/m<1or f(n/m))or f(n,m*10))
Try it online!
add a comment |Â
up vote
2
down vote
Python 2, 72 bytes
f=lambda n,m=10:n>=m/10and(n%m&~-(n%m)<1and(n/m<1or f(n/m))or f(n,m*10))
Try it online!
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Python 2, 72 bytes
f=lambda n,m=10:n>=m/10and(n%m&~-(n%m)<1and(n/m<1or f(n/m))or f(n,m*10))
Try it online!
Python 2, 72 bytes
f=lambda n,m=10:n>=m/10and(n%m&~-(n%m)<1and(n/m<1or f(n/m))or f(n,m*10))
Try it online!
answered 1 hour ago
ovs
17.7k21058
17.7k21058
add a comment |Â
add a comment |Â
up vote
2
down vote
JavaScript (Node.js), 75 69 bytes
-6 bytes thanks @Arnauld. At most 32-bit support
f=x=>+x?[...x].some((_,i)=>(y=x.slice(0,++i))&~-y?0:f(x.slice(i))):!x
Try it online!
Input as a string.
@Arnauld Oh dang I forgot the bitwise trick again! Thanks!
– Shieru Asakoto
1 hour ago
add a comment |Â
up vote
2
down vote
JavaScript (Node.js), 75 69 bytes
-6 bytes thanks @Arnauld. At most 32-bit support
f=x=>+x?[...x].some((_,i)=>(y=x.slice(0,++i))&~-y?0:f(x.slice(i))):!x
Try it online!
Input as a string.
@Arnauld Oh dang I forgot the bitwise trick again! Thanks!
– Shieru Asakoto
1 hour ago
add a comment |Â
up vote
2
down vote
up vote
2
down vote
JavaScript (Node.js), 75 69 bytes
-6 bytes thanks @Arnauld. At most 32-bit support
f=x=>+x?[...x].some((_,i)=>(y=x.slice(0,++i))&~-y?0:f(x.slice(i))):!x
Try it online!
Input as a string.
JavaScript (Node.js), 75 69 bytes
-6 bytes thanks @Arnauld. At most 32-bit support
f=x=>+x?[...x].some((_,i)=>(y=x.slice(0,++i))&~-y?0:f(x.slice(i))):!x
Try it online!
Input as a string.
edited 46 mins ago
answered 1 hour ago
Shieru Asakoto
1,980312
1,980312
@Arnauld Oh dang I forgot the bitwise trick again! Thanks!
– Shieru Asakoto
1 hour ago
add a comment |Â
@Arnauld Oh dang I forgot the bitwise trick again! Thanks!
– Shieru Asakoto
1 hour ago
@Arnauld Oh dang I forgot the bitwise trick again! Thanks!
– Shieru Asakoto
1 hour ago
@Arnauld Oh dang I forgot the bitwise trick again! Thanks!
– Shieru Asakoto
1 hour ago
add a comment |Â
up vote
1
down vote
Jelly, 9 bytes
ŒṖḌl2ĊƑ€Ẹ
Check out the test suite!
How?
ŒṖḌl2ĊƑ€Ẹ Full program. N = integer input.
ŒṖ All possible partitions of the digits of N.
Ḍ Undecimal (i.e. join to numbers).
l2 Log2. Note: returns -(-inf+nanj) for 0, so it doesn't fail.
ĊƑ€ For each, check if the logarithm equals its ceil.
Ẹ Any. Return 0 if there are no truthy elements, 1 otherwise.
add a comment |Â
up vote
1
down vote
Jelly, 9 bytes
ŒṖḌl2ĊƑ€Ẹ
Check out the test suite!
How?
ŒṖḌl2ĊƑ€Ẹ Full program. N = integer input.
ŒṖ All possible partitions of the digits of N.
Ḍ Undecimal (i.e. join to numbers).
l2 Log2. Note: returns -(-inf+nanj) for 0, so it doesn't fail.
ĊƑ€ For each, check if the logarithm equals its ceil.
Ẹ Any. Return 0 if there are no truthy elements, 1 otherwise.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Jelly, 9 bytes
ŒṖḌl2ĊƑ€Ẹ
Check out the test suite!
How?
ŒṖḌl2ĊƑ€Ẹ Full program. N = integer input.
ŒṖ All possible partitions of the digits of N.
Ḍ Undecimal (i.e. join to numbers).
l2 Log2. Note: returns -(-inf+nanj) for 0, so it doesn't fail.
ĊƑ€ For each, check if the logarithm equals its ceil.
Ẹ Any. Return 0 if there are no truthy elements, 1 otherwise.
Jelly, 9 bytes
ŒṖḌl2ĊƑ€Ẹ
Check out the test suite!
How?
ŒṖḌl2ĊƑ€Ẹ Full program. N = integer input.
ŒṖ All possible partitions of the digits of N.
Ḍ Undecimal (i.e. join to numbers).
l2 Log2. Note: returns -(-inf+nanj) for 0, so it doesn't fail.
ĊƑ€ For each, check if the logarithm equals its ceil.
Ẹ Any. Return 0 if there are no truthy elements, 1 otherwise.
edited 2 mins ago
answered 10 mins ago


Mr. Xcoder
30.8k758195
30.8k758195
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f173833%2fcan-the-number-be-split-into-powers-of-2%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Any limit to how many digits must be supported? Is it OK if it works in theory (assume much memory/time) for large numbers? The reason why I ask: A possible solution is to create all possible "splits" (don't remember the word).
164 -> "1,6,4", "1, 64", "16,4", "164"
. The number of "splits" can get quite large for large numbers.– Stewie Griffin
1 hour ago
@StewieGriffin initially I thought about limiting the input number to the range of a standard
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.– Charlie
1 hour ago
2
Suggested test case:
101
(falsy because of the 0) ... or should this still be true (1 - 01
)?– Shieru Asakoto
56 mins ago
1
@ShieruAsakoto I've been testing the
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.– Charlie
53 mins ago
1
Just leaving this here as tip for everyone. Here are three possible ways to check if a number is a power of 2: 1) Check if
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.– Kevin Cruijssen
37 mins ago