Can the number be splitted into powers of 2?
Clash Royale CLAN TAG#URR8PPP
up vote
4
down vote
favorite
Yesterday while playing with my kid I noticed the number in his toy train:
So we have $$4281$$ that can be splitted 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 splitted 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-024)
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
 |Â
show 1 more comment
up vote
4
down vote
favorite
Yesterday while playing with my kid I noticed the number in his toy train:
So we have $$4281$$ that can be splitted 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 splitted 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-024)
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
38 mins 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
20 mins ago
2
Suggested test case:101
(falsy because of the 0) ... or should this still be true (1 - 01
)?
– Shieru Asakoto
6 mins ago
Can81024
only be8-1024
, or is8-1-024
also valid? EDIT: Basically the same question whether leading 0s are allowed as for @ShieruAsakoto suggested test case above..
– Kevin Cruijssen
4 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
3 mins ago
 |Â
show 1 more comment
up vote
4
down vote
favorite
up vote
4
down vote
favorite
Yesterday while playing with my kid I noticed the number in his toy train:
So we have $$4281$$ that can be splitted 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 splitted 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-024)
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 splitted 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 splitted 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-024)
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 2 mins ago
asked 59 mins ago


Charlie
6,8271979
6,8271979
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
38 mins 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
20 mins ago
2
Suggested test case:101
(falsy because of the 0) ... or should this still be true (1 - 01
)?
– Shieru Asakoto
6 mins ago
Can81024
only be8-1024
, or is8-1-024
also valid? EDIT: Basically the same question whether leading 0s are allowed as for @ShieruAsakoto suggested test case above..
– Kevin Cruijssen
4 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
3 mins ago
 |Â
show 1 more 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
38 mins 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
20 mins ago
2
Suggested test case:101
(falsy because of the 0) ... or should this still be true (1 - 01
)?
– Shieru Asakoto
6 mins ago
Can81024
only be8-1024
, or is8-1-024
also valid? EDIT: Basically the same question whether leading 0s are allowed as for @ShieruAsakoto suggested test case above..
– Kevin Cruijssen
4 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
3 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
38 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
38 mins 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
20 mins 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
20 mins ago
2
2
Suggested test case:
101
(falsy because of the 0) ... or should this still be true (1 - 01
)?– Shieru Asakoto
6 mins ago
Suggested test case:
101
(falsy because of the 0) ... or should this still be true (1 - 01
)?– Shieru Asakoto
6 mins ago
Can
81024
only be 8-1024
, or is 8-1-024
also valid? EDIT: Basically the same question whether leading 0s are allowed as for @ShieruAsakoto suggested test case above..– Kevin Cruijssen
4 mins ago
Can
81024
only be 8-1024
, or is 8-1-024
also valid? EDIT: Basically the same question whether leading 0s are allowed as for @ShieruAsakoto suggested test case above..– Kevin Cruijssen
4 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
3 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
3 mins ago
 |Â
show 1 more comment
5 Answers
5
active
oldest
votes
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
JavaScript (Node.js), 75 70 bytes
-5 bytes thanks @Arnauld. At most 32-bit support
f=x=>+x?[...x].some((_,i)=>!((y=x.slice(0,++i))&~-y)&f(x.slice(i))):!x
Try it online!
Input as a string.
@Arnauld Oh dang I forgot the bitwise trick again! Thanks!
– Shieru Asakoto
18 mins ago
Using a ternary saves 1 byte:(y=x.slice(0,++i))&~-y?0:f(x.slice(i))
.
– Arnauld
2 mins ago
add a comment |Â
up vote
1
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
1
down vote
JavaScript (Node.js), 69 64 bytes
f=(x,m=10,q=!(x%m&x%m-1|x%m<m/10))=>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
.
-5 bytes by golfing the power-of-2 check. Also rejects leading zeroes, e.g. 101 where 1/01
is an invalid partition.
add a comment |Â
up vote
0
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)
add a comment |Â
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 52 mins ago


TFeld
12.4k2834
12.4k2834
add a comment |Â
add a comment |Â
up vote
2
down vote
JavaScript (Node.js), 75 70 bytes
-5 bytes thanks @Arnauld. At most 32-bit support
f=x=>+x?[...x].some((_,i)=>!((y=x.slice(0,++i))&~-y)&f(x.slice(i))):!x
Try it online!
Input as a string.
@Arnauld Oh dang I forgot the bitwise trick again! Thanks!
– Shieru Asakoto
18 mins ago
Using a ternary saves 1 byte:(y=x.slice(0,++i))&~-y?0:f(x.slice(i))
.
– Arnauld
2 mins ago
add a comment |Â
up vote
2
down vote
JavaScript (Node.js), 75 70 bytes
-5 bytes thanks @Arnauld. At most 32-bit support
f=x=>+x?[...x].some((_,i)=>!((y=x.slice(0,++i))&~-y)&f(x.slice(i))):!x
Try it online!
Input as a string.
@Arnauld Oh dang I forgot the bitwise trick again! Thanks!
– Shieru Asakoto
18 mins ago
Using a ternary saves 1 byte:(y=x.slice(0,++i))&~-y?0:f(x.slice(i))
.
– Arnauld
2 mins ago
add a comment |Â
up vote
2
down vote
up vote
2
down vote
JavaScript (Node.js), 75 70 bytes
-5 bytes thanks @Arnauld. At most 32-bit support
f=x=>+x?[...x].some((_,i)=>!((y=x.slice(0,++i))&~-y)&f(x.slice(i))):!x
Try it online!
Input as a string.
JavaScript (Node.js), 75 70 bytes
-5 bytes thanks @Arnauld. At most 32-bit support
f=x=>+x?[...x].some((_,i)=>!((y=x.slice(0,++i))&~-y)&f(x.slice(i))):!x
Try it online!
Input as a string.
edited 4 mins ago
answered 32 mins ago
Shieru Asakoto
1,980312
1,980312
@Arnauld Oh dang I forgot the bitwise trick again! Thanks!
– Shieru Asakoto
18 mins ago
Using a ternary saves 1 byte:(y=x.slice(0,++i))&~-y?0:f(x.slice(i))
.
– Arnauld
2 mins ago
add a comment |Â
@Arnauld Oh dang I forgot the bitwise trick again! Thanks!
– Shieru Asakoto
18 mins ago
Using a ternary saves 1 byte:(y=x.slice(0,++i))&~-y?0:f(x.slice(i))
.
– Arnauld
2 mins ago
@Arnauld Oh dang I forgot the bitwise trick again! Thanks!
– Shieru Asakoto
18 mins ago
@Arnauld Oh dang I forgot the bitwise trick again! Thanks!
– Shieru Asakoto
18 mins ago
Using a ternary saves 1 byte:
(y=x.slice(0,++i))&~-y?0:f(x.slice(i))
.– Arnauld
2 mins ago
Using a ternary saves 1 byte:
(y=x.slice(0,++i))&~-y?0:f(x.slice(i))
.– Arnauld
2 mins ago
add a comment |Â
up vote
1
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
1
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
1
down vote
up vote
1
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 12 mins ago
ovs
17.6k21058
17.6k21058
add a comment |Â
add a comment |Â
up vote
1
down vote
JavaScript (Node.js), 69 64 bytes
f=(x,m=10,q=!(x%m&x%m-1|x%m<m/10))=>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
.
-5 bytes by golfing the power-of-2 check. Also rejects leading zeroes, e.g. 101 where 1/01
is an invalid partition.
add a comment |Â
up vote
1
down vote
JavaScript (Node.js), 69 64 bytes
f=(x,m=10,q=!(x%m&x%m-1|x%m<m/10))=>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
.
-5 bytes by golfing the power-of-2 check. Also rejects leading zeroes, e.g. 101 where 1/01
is an invalid partition.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
JavaScript (Node.js), 69 64 bytes
f=(x,m=10,q=!(x%m&x%m-1|x%m<m/10))=>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
.
-5 bytes by golfing the power-of-2 check. Also rejects leading zeroes, e.g. 101 where 1/01
is an invalid partition.
JavaScript (Node.js), 69 64 bytes
f=(x,m=10,q=!(x%m&x%m-1|x%m<m/10))=>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
.
-5 bytes by golfing the power-of-2 check. Also rejects leading zeroes, e.g. 101 where 1/01
is an invalid partition.
edited 2 mins ago
answered 15 mins ago


Bubbler
3,917541
3,917541
add a comment |Â
add a comment |Â
up vote
0
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)
add a comment |Â
up vote
0
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)
add a comment |Â
up vote
0
down vote
up vote
0
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 5 mins ago


Kevin Cruijssen
31.5k553171
31.5k553171
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-splitted-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
38 mins 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
20 mins ago
2
Suggested test case:
101
(falsy because of the 0) ... or should this still be true (1 - 01
)?– Shieru Asakoto
6 mins ago
Can
81024
only be8-1024
, or is8-1-024
also valid? EDIT: Basically the same question whether leading 0s are allowed as for @ShieruAsakoto suggested test case above..– Kevin Cruijssen
4 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
3 mins ago