Can the number be splitted into powers of 2?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
4
down vote

favorite












Yesterday while playing with my kid I noticed the number in his toy train:



4281



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!










share|improve this question























  • 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 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




    @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














up vote
4
down vote

favorite












Yesterday while playing with my kid I noticed the number in his toy train:



4281



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!










share|improve this question























  • 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 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




    @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












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:



4281



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!










share|improve this question















Yesterday while playing with my kid I noticed the number in his toy train:



4281



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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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 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




    @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
















  • 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 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




    @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















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










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!






share|improve this answer



























    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.






    share|improve this answer






















    • @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

















    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!






    share|improve this answer



























      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.






      share|improve this answer





























        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)




        share




















          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "200"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          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






























          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!






          share|improve this answer
























            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!






            share|improve this answer






















              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!






              share|improve this answer













              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!







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered 52 mins ago









              TFeld

              12.4k2834




              12.4k2834




















                  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.






                  share|improve this answer






















                  • @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














                  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.






                  share|improve this answer






















                  • @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












                  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.






                  share|improve this answer















                  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.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  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
















                  • @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










                  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!






                  share|improve this answer
























                    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!






                    share|improve this answer






















                      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!






                      share|improve this answer













                      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!







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered 12 mins ago









                      ovs

                      17.6k21058




                      17.6k21058




















                          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.






                          share|improve this answer


























                            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.






                            share|improve this answer
























                              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.






                              share|improve this answer















                              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.







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited 2 mins ago

























                              answered 15 mins ago









                              Bubbler

                              3,917541




                              3,917541




















                                  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)




                                  share
























                                    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)




                                    share






















                                      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)




                                      share













                                      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)





                                      share











                                      share


                                      share










                                      answered 5 mins ago









                                      Kevin Cruijssen

                                      31.5k553171




                                      31.5k553171



























                                           

                                          draft saved


                                          draft discarded















































                                           


                                          draft saved


                                          draft discarded














                                          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













































































                                          Comments

                                          Popular posts from this blog

                                          What does second last employer means? [closed]

                                          List of Gilmore Girls characters

                                          Confectionery