Split the bits!

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











up vote
17
down vote

favorite












We define $V(x)$ as the list of distinct powers of $2$ that sum to $x$. For instance, $V(35)=[32,2,1]$.



By convention, powers are sorted here from highest to lowest. But it does not affect the logic of the challenge, nor the expected solutions.



Task



Given a semiprime $N$, replace each term in $V(N)$ with another list of powers of $2$ that sum to this term, in such a way that the union of all resulting sub-lists is an exact cover of the matrix $M$ defined as:



$$M_i,j=V(P)_i times V(Q)_j$$



where $P$ and $Q$ are the prime factors of $N$.



This is much easier to understand with some examples.



Example #1



For $N=21$, we have:



  • $V(N)=[16,4,1]$

  • $P=7$ and $V(P)=[4,2,1]$

  • $Q=3$ and $V(Q)=[2,1]$

  • $M=pmatrix8&4&2\4&2&1$

To turn $V(N)$ into an exact cover of $M$, we may split $16$ into $8+4+4$ and $4$ into $2+2$, while $1$ is left unchanged. So a possible output is:



$$[ [ 8, 4, 4 ], [ 2, 2 ], [ 1 ] ]$$



Another valid output is:



$$[ [ 8, 4, 2, 2 ], [ 4 ], [ 1 ] ]$$



Example #2



For $N=851$, we have:



  • $V(N)=[512,256,64,16,2,1]$

  • $P=37$ and $V(P)=[32,4,1]$

  • $Q=23$ and $V(Q)=[16,4,2,1]$

  • $M=pmatrix512&64&16\128&16&4\64&8&2\32&4&1$

A possible output is:



$$[ [ 512 ], [ 128, 64, 64 ], [ 32, 16, 16 ], [ 8, 4, 4 ], [ 2 ], [ 1 ] ]$$



Rules



  • Because factorizing $N$ is not the main part of the challenge, you may alternately take $P$ and $Q$ as input.

  • When several possible solutions exist, you may either return just one of them or all of them.

  • You may alternately return the exponents of the powers (e.g. $[[3,2,2],[1,1],[0]]$ instead of $[[8,4,4],[2,2],[1]]$).

  • The order of the sub-lists doesn't matter, nor does the order of the terms in each sub-list.

  • For some semiprimes, you won't have to split any term because $V(N)$ already is a perfect cover of $M$ (see A235040). But you still have to return a list of (singleton) lists such as $[[8],[4],[2],[1]]$ for $N=15$.

  • This is code-golf!

Test cases



 Input | Possible output
-------+-----------------------------------------------------------------------------
9 | [ [ 4, 2, 2 ], [ 1 ] ]
15 | [ [ 8 ], [ 4 ], [ 2 ], [ 1 ] ]
21 | [ [ 8, 4, 4 ], [ 2, 2 ], [ 1 ] ]
51 | [ [ 32 ], [ 16 ], [ 2 ], [ 1 ] ]
129 | [ [ 64, 32, 16, 8, 4, 2, 2 ], [ 1 ] ]
159 | [ [ 64, 32, 32 ], [ 16 ], [ 8 ], [ 4 ], [ 2 ], [ 1 ] ]
161 | [ [ 64, 32, 16, 16 ], [ 8, 8, 4, 4, 4, 2, 2 ], [ 1 ] ]
201 | [ [ 128 ], [ 64 ], [ 4, 2, 2 ], [ 1 ] ]
403 | [ [ 128, 64, 64 ], [ 32, 32, 16, 16, 16, 8, 8 ], [ 8, 4, 4 ], [ 2 ], [ 1 ] ]
851 | [ [ 512 ], [ 128, 64, 64 ], [ 32, 16, 16 ], [ 8, 4, 4 ], [ 2 ], [ 1 ] ]
2307 | [ [ 1024, 512, 512 ], [ 256 ], [ 2 ], [ 1 ] ]






share|improve this question





















  • can we take P and Q instead of N?
    – ngn
    Aug 6 at 15:29










  • @ngn I'm going to say yes, because factorizing N is not the main part of the challenge.
    – Arnauld
    Aug 6 at 15:33






  • 1




    May we return the output flattened?
    – Erik the Outgolfer
    Aug 6 at 19:06










  • @EriktheOutgolfer ... The output flattened is just a partition of the input (1+2+2+4=9, for example). I don't think it should be allowed
    – Mr. Xcoder
    Aug 6 at 19:08










  • @EriktheOutgolfer I don't think it could be unambiguous this way, as the last term of a sub-list may be the same as the first term of the next one.
    – Arnauld
    Aug 6 at 19:08














up vote
17
down vote

favorite












We define $V(x)$ as the list of distinct powers of $2$ that sum to $x$. For instance, $V(35)=[32,2,1]$.



By convention, powers are sorted here from highest to lowest. But it does not affect the logic of the challenge, nor the expected solutions.



Task



Given a semiprime $N$, replace each term in $V(N)$ with another list of powers of $2$ that sum to this term, in such a way that the union of all resulting sub-lists is an exact cover of the matrix $M$ defined as:



$$M_i,j=V(P)_i times V(Q)_j$$



where $P$ and $Q$ are the prime factors of $N$.



This is much easier to understand with some examples.



Example #1



For $N=21$, we have:



  • $V(N)=[16,4,1]$

  • $P=7$ and $V(P)=[4,2,1]$

  • $Q=3$ and $V(Q)=[2,1]$

  • $M=pmatrix8&4&2\4&2&1$

To turn $V(N)$ into an exact cover of $M$, we may split $16$ into $8+4+4$ and $4$ into $2+2$, while $1$ is left unchanged. So a possible output is:



$$[ [ 8, 4, 4 ], [ 2, 2 ], [ 1 ] ]$$



Another valid output is:



$$[ [ 8, 4, 2, 2 ], [ 4 ], [ 1 ] ]$$



Example #2



For $N=851$, we have:



  • $V(N)=[512,256,64,16,2,1]$

  • $P=37$ and $V(P)=[32,4,1]$

  • $Q=23$ and $V(Q)=[16,4,2,1]$

  • $M=pmatrix512&64&16\128&16&4\64&8&2\32&4&1$

A possible output is:



$$[ [ 512 ], [ 128, 64, 64 ], [ 32, 16, 16 ], [ 8, 4, 4 ], [ 2 ], [ 1 ] ]$$



Rules



  • Because factorizing $N$ is not the main part of the challenge, you may alternately take $P$ and $Q$ as input.

  • When several possible solutions exist, you may either return just one of them or all of them.

  • You may alternately return the exponents of the powers (e.g. $[[3,2,2],[1,1],[0]]$ instead of $[[8,4,4],[2,2],[1]]$).

  • The order of the sub-lists doesn't matter, nor does the order of the terms in each sub-list.

  • For some semiprimes, you won't have to split any term because $V(N)$ already is a perfect cover of $M$ (see A235040). But you still have to return a list of (singleton) lists such as $[[8],[4],[2],[1]]$ for $N=15$.

  • This is code-golf!

Test cases



 Input | Possible output
-------+-----------------------------------------------------------------------------
9 | [ [ 4, 2, 2 ], [ 1 ] ]
15 | [ [ 8 ], [ 4 ], [ 2 ], [ 1 ] ]
21 | [ [ 8, 4, 4 ], [ 2, 2 ], [ 1 ] ]
51 | [ [ 32 ], [ 16 ], [ 2 ], [ 1 ] ]
129 | [ [ 64, 32, 16, 8, 4, 2, 2 ], [ 1 ] ]
159 | [ [ 64, 32, 32 ], [ 16 ], [ 8 ], [ 4 ], [ 2 ], [ 1 ] ]
161 | [ [ 64, 32, 16, 16 ], [ 8, 8, 4, 4, 4, 2, 2 ], [ 1 ] ]
201 | [ [ 128 ], [ 64 ], [ 4, 2, 2 ], [ 1 ] ]
403 | [ [ 128, 64, 64 ], [ 32, 32, 16, 16, 16, 8, 8 ], [ 8, 4, 4 ], [ 2 ], [ 1 ] ]
851 | [ [ 512 ], [ 128, 64, 64 ], [ 32, 16, 16 ], [ 8, 4, 4 ], [ 2 ], [ 1 ] ]
2307 | [ [ 1024, 512, 512 ], [ 256 ], [ 2 ], [ 1 ] ]






share|improve this question





















  • can we take P and Q instead of N?
    – ngn
    Aug 6 at 15:29










  • @ngn I'm going to say yes, because factorizing N is not the main part of the challenge.
    – Arnauld
    Aug 6 at 15:33






  • 1




    May we return the output flattened?
    – Erik the Outgolfer
    Aug 6 at 19:06










  • @EriktheOutgolfer ... The output flattened is just a partition of the input (1+2+2+4=9, for example). I don't think it should be allowed
    – Mr. Xcoder
    Aug 6 at 19:08










  • @EriktheOutgolfer I don't think it could be unambiguous this way, as the last term of a sub-list may be the same as the first term of the next one.
    – Arnauld
    Aug 6 at 19:08












up vote
17
down vote

favorite









up vote
17
down vote

favorite











We define $V(x)$ as the list of distinct powers of $2$ that sum to $x$. For instance, $V(35)=[32,2,1]$.



By convention, powers are sorted here from highest to lowest. But it does not affect the logic of the challenge, nor the expected solutions.



Task



Given a semiprime $N$, replace each term in $V(N)$ with another list of powers of $2$ that sum to this term, in such a way that the union of all resulting sub-lists is an exact cover of the matrix $M$ defined as:



$$M_i,j=V(P)_i times V(Q)_j$$



where $P$ and $Q$ are the prime factors of $N$.



This is much easier to understand with some examples.



Example #1



For $N=21$, we have:



  • $V(N)=[16,4,1]$

  • $P=7$ and $V(P)=[4,2,1]$

  • $Q=3$ and $V(Q)=[2,1]$

  • $M=pmatrix8&4&2\4&2&1$

To turn $V(N)$ into an exact cover of $M$, we may split $16$ into $8+4+4$ and $4$ into $2+2$, while $1$ is left unchanged. So a possible output is:



$$[ [ 8, 4, 4 ], [ 2, 2 ], [ 1 ] ]$$



Another valid output is:



$$[ [ 8, 4, 2, 2 ], [ 4 ], [ 1 ] ]$$



Example #2



For $N=851$, we have:



  • $V(N)=[512,256,64,16,2,1]$

  • $P=37$ and $V(P)=[32,4,1]$

  • $Q=23$ and $V(Q)=[16,4,2,1]$

  • $M=pmatrix512&64&16\128&16&4\64&8&2\32&4&1$

A possible output is:



$$[ [ 512 ], [ 128, 64, 64 ], [ 32, 16, 16 ], [ 8, 4, 4 ], [ 2 ], [ 1 ] ]$$



Rules



  • Because factorizing $N$ is not the main part of the challenge, you may alternately take $P$ and $Q$ as input.

  • When several possible solutions exist, you may either return just one of them or all of them.

  • You may alternately return the exponents of the powers (e.g. $[[3,2,2],[1,1],[0]]$ instead of $[[8,4,4],[2,2],[1]]$).

  • The order of the sub-lists doesn't matter, nor does the order of the terms in each sub-list.

  • For some semiprimes, you won't have to split any term because $V(N)$ already is a perfect cover of $M$ (see A235040). But you still have to return a list of (singleton) lists such as $[[8],[4],[2],[1]]$ for $N=15$.

  • This is code-golf!

Test cases



 Input | Possible output
-------+-----------------------------------------------------------------------------
9 | [ [ 4, 2, 2 ], [ 1 ] ]
15 | [ [ 8 ], [ 4 ], [ 2 ], [ 1 ] ]
21 | [ [ 8, 4, 4 ], [ 2, 2 ], [ 1 ] ]
51 | [ [ 32 ], [ 16 ], [ 2 ], [ 1 ] ]
129 | [ [ 64, 32, 16, 8, 4, 2, 2 ], [ 1 ] ]
159 | [ [ 64, 32, 32 ], [ 16 ], [ 8 ], [ 4 ], [ 2 ], [ 1 ] ]
161 | [ [ 64, 32, 16, 16 ], [ 8, 8, 4, 4, 4, 2, 2 ], [ 1 ] ]
201 | [ [ 128 ], [ 64 ], [ 4, 2, 2 ], [ 1 ] ]
403 | [ [ 128, 64, 64 ], [ 32, 32, 16, 16, 16, 8, 8 ], [ 8, 4, 4 ], [ 2 ], [ 1 ] ]
851 | [ [ 512 ], [ 128, 64, 64 ], [ 32, 16, 16 ], [ 8, 4, 4 ], [ 2 ], [ 1 ] ]
2307 | [ [ 1024, 512, 512 ], [ 256 ], [ 2 ], [ 1 ] ]






share|improve this question













We define $V(x)$ as the list of distinct powers of $2$ that sum to $x$. For instance, $V(35)=[32,2,1]$.



By convention, powers are sorted here from highest to lowest. But it does not affect the logic of the challenge, nor the expected solutions.



Task



Given a semiprime $N$, replace each term in $V(N)$ with another list of powers of $2$ that sum to this term, in such a way that the union of all resulting sub-lists is an exact cover of the matrix $M$ defined as:



$$M_i,j=V(P)_i times V(Q)_j$$



where $P$ and $Q$ are the prime factors of $N$.



This is much easier to understand with some examples.



Example #1



For $N=21$, we have:



  • $V(N)=[16,4,1]$

  • $P=7$ and $V(P)=[4,2,1]$

  • $Q=3$ and $V(Q)=[2,1]$

  • $M=pmatrix8&4&2\4&2&1$

To turn $V(N)$ into an exact cover of $M$, we may split $16$ into $8+4+4$ and $4$ into $2+2$, while $1$ is left unchanged. So a possible output is:



$$[ [ 8, 4, 4 ], [ 2, 2 ], [ 1 ] ]$$



Another valid output is:



$$[ [ 8, 4, 2, 2 ], [ 4 ], [ 1 ] ]$$



Example #2



For $N=851$, we have:



  • $V(N)=[512,256,64,16,2,1]$

  • $P=37$ and $V(P)=[32,4,1]$

  • $Q=23$ and $V(Q)=[16,4,2,1]$

  • $M=pmatrix512&64&16\128&16&4\64&8&2\32&4&1$

A possible output is:



$$[ [ 512 ], [ 128, 64, 64 ], [ 32, 16, 16 ], [ 8, 4, 4 ], [ 2 ], [ 1 ] ]$$



Rules



  • Because factorizing $N$ is not the main part of the challenge, you may alternately take $P$ and $Q$ as input.

  • When several possible solutions exist, you may either return just one of them or all of them.

  • You may alternately return the exponents of the powers (e.g. $[[3,2,2],[1,1],[0]]$ instead of $[[8,4,4],[2,2],[1]]$).

  • The order of the sub-lists doesn't matter, nor does the order of the terms in each sub-list.

  • For some semiprimes, you won't have to split any term because $V(N)$ already is a perfect cover of $M$ (see A235040). But you still have to return a list of (singleton) lists such as $[[8],[4],[2],[1]]$ for $N=15$.

  • This is code-golf!

Test cases



 Input | Possible output
-------+-----------------------------------------------------------------------------
9 | [ [ 4, 2, 2 ], [ 1 ] ]
15 | [ [ 8 ], [ 4 ], [ 2 ], [ 1 ] ]
21 | [ [ 8, 4, 4 ], [ 2, 2 ], [ 1 ] ]
51 | [ [ 32 ], [ 16 ], [ 2 ], [ 1 ] ]
129 | [ [ 64, 32, 16, 8, 4, 2, 2 ], [ 1 ] ]
159 | [ [ 64, 32, 32 ], [ 16 ], [ 8 ], [ 4 ], [ 2 ], [ 1 ] ]
161 | [ [ 64, 32, 16, 16 ], [ 8, 8, 4, 4, 4, 2, 2 ], [ 1 ] ]
201 | [ [ 128 ], [ 64 ], [ 4, 2, 2 ], [ 1 ] ]
403 | [ [ 128, 64, 64 ], [ 32, 32, 16, 16, 16, 8, 8 ], [ 8, 4, 4 ], [ 2 ], [ 1 ] ]
851 | [ [ 512 ], [ 128, 64, 64 ], [ 32, 16, 16 ], [ 8, 4, 4 ], [ 2 ], [ 1 ] ]
2307 | [ [ 1024, 512, 512 ], [ 256 ], [ 2 ], [ 1 ] ]








share|improve this question












share|improve this question




share|improve this question








edited Aug 6 at 15:34
























asked Aug 6 at 15:02









Arnauld

61.6k575258




61.6k575258











  • can we take P and Q instead of N?
    – ngn
    Aug 6 at 15:29










  • @ngn I'm going to say yes, because factorizing N is not the main part of the challenge.
    – Arnauld
    Aug 6 at 15:33






  • 1




    May we return the output flattened?
    – Erik the Outgolfer
    Aug 6 at 19:06










  • @EriktheOutgolfer ... The output flattened is just a partition of the input (1+2+2+4=9, for example). I don't think it should be allowed
    – Mr. Xcoder
    Aug 6 at 19:08










  • @EriktheOutgolfer I don't think it could be unambiguous this way, as the last term of a sub-list may be the same as the first term of the next one.
    – Arnauld
    Aug 6 at 19:08
















  • can we take P and Q instead of N?
    – ngn
    Aug 6 at 15:29










  • @ngn I'm going to say yes, because factorizing N is not the main part of the challenge.
    – Arnauld
    Aug 6 at 15:33






  • 1




    May we return the output flattened?
    – Erik the Outgolfer
    Aug 6 at 19:06










  • @EriktheOutgolfer ... The output flattened is just a partition of the input (1+2+2+4=9, for example). I don't think it should be allowed
    – Mr. Xcoder
    Aug 6 at 19:08










  • @EriktheOutgolfer I don't think it could be unambiguous this way, as the last term of a sub-list may be the same as the first term of the next one.
    – Arnauld
    Aug 6 at 19:08















can we take P and Q instead of N?
– ngn
Aug 6 at 15:29




can we take P and Q instead of N?
– ngn
Aug 6 at 15:29












@ngn I'm going to say yes, because factorizing N is not the main part of the challenge.
– Arnauld
Aug 6 at 15:33




@ngn I'm going to say yes, because factorizing N is not the main part of the challenge.
– Arnauld
Aug 6 at 15:33




1




1




May we return the output flattened?
– Erik the Outgolfer
Aug 6 at 19:06




May we return the output flattened?
– Erik the Outgolfer
Aug 6 at 19:06












@EriktheOutgolfer ... The output flattened is just a partition of the input (1+2+2+4=9, for example). I don't think it should be allowed
– Mr. Xcoder
Aug 6 at 19:08




@EriktheOutgolfer ... The output flattened is just a partition of the input (1+2+2+4=9, for example). I don't think it should be allowed
– Mr. Xcoder
Aug 6 at 19:08












@EriktheOutgolfer I don't think it could be unambiguous this way, as the last term of a sub-list may be the same as the first term of the next one.
– Arnauld
Aug 6 at 19:08




@EriktheOutgolfer I don't think it could be unambiguous this way, as the last term of a sub-list may be the same as the first term of the next one.
– Arnauld
Aug 6 at 19:08










7 Answers
7






active

oldest

votes

















up vote
4
down vote














K (ngn/k), 66 63 bytes



a)?+b)_b:b@>b:,/*/:/2#a:*/'(&'x,*/x


Try it online!



accepts (P;Q) instead of N



algorithm:



  • compute A as the partial sums of V(P*Q)


  • multiply each V(P) with each V(Q), sort the products in descending order (let's call that R), and compute their partial sums B


  • find the positions of those elements in B that also occur in A; cut R right after those positions






share|improve this answer






























    up vote
    3
    down vote














    Jelly, 24 bytes



    BṚT’2*
    Ç€×þ/FṢŒṖ§⁼Ç}ɗƇPḢ


    A monadic link accepting a list of two integers [P, Q] which yields one possible list of lists as described in the question.



    Try it online! (footer prints a string representation to show the list as it really is)



    Or see the test-suite (taking a list of N and reordering results to be like those in the question)



    How?



    We may always slice up the elements of $M$ from lowest up, greedily (either there is a $1$ in $M$ or we had an input of $4$, when $M=[[4]]$) in order to find a solution.



    Note: the code collects all (one!) such solutions in a list and then takes the head (only) result - i.e. the final head is necessary as the partitions are not of all possible orderings.



    BṚT’2* - Link 1, powers of 2 that sum to N: integer, N e.g. 105
    B - binary [1,1,0,1,0,0,1]
    Ṛ - reverse [1,0,0,1,0,1,1]
    T - truthy indices [1,4,6,7]
    ’ - decrement [0,3,5,6]
    2 - literal two 2
    * - exponentiate [1,8,32,64]

    Ç€×þ/FṢŒṖ§⁼Ç}ɗƇPḢ - Main Link: list of two integers, [P,Q]
    Ç€ - call last Link (1) as a monad for €ach
    / - reduce with:
    þ - table with:
    × - multiplication
    F - flatten
    á¹¢ - sort
    ŒṖ - all partitions
    Ƈ - filter keep if:
    ɗ - last three links as a dyad:
    § - sum each
    } - use right...
    P - ...value: product (i.e. P×Q)
    Ç - ...do: call last Link (1) as a monad
    ⁼ - equal? (non-vectorising so "all equal?")
    Ḣ - head





    share|improve this answer




























      up vote
      3
      down vote














      Python 2, 261 233 232 231 bytes





      g=lambda n,r=,i=1:n and g(n/2,[i]*(n&1)+r,i*2)or r
      def f(p,q):
      V=[[v]for v in g(p*q)];i=j=0
      for m in sorted(-a*b for a in g(p)for b in g(q)):
      v=V[i]
      while-m<v[j]:v[j:j+1]=[v[j]/2]*2
      i,j=[i+1,i,0,j+1][j+1<len(v)::2]
      return V


      Try it online!



      1 byte from Jo King; and another 1 byte due to Kevin Cruijssen.



      Takes as input p,q. Pursues the greedy algorithm.






      share|improve this answer























      • -k-1 can be ~k.
        – Jonathan Frech
        Aug 7 at 14:33










      • The i,j assignment can be i,j=[i+1,i,0,j+1][j+1<len(v)::2] for -1 byte
        – Jo King
        Aug 8 at 7:20










      • @Jo King: Hahaha! That is twisted!
        – Chas Brown
        Aug 8 at 7:24










      • while v[j]>-m can be while-m<v[j]
        – Kevin Cruijssen
        Aug 8 at 7:29










      • @Kevin Cruijssen: Yes, indeed. Thx!
        – Chas Brown
        Aug 8 at 7:32

















      up vote
      2
      down vote














      Jelly, 41 bytes



      Œṗl2ĊƑ$Ƈ
      PÇIP$ƇṪÇ€Œpµ³ÇIP$ƇṪƊ€ŒpZPṢ⁼FṢ$µƇ


      Try it online!



      Should probably be much shorter (some parts feel very repetitive; especially ÇIP$Ƈ, but I don't know how to golf it). Explanation to come after further golfing. Returns all possible solutions in case multiple exist and takes input as $[P, Q]$.






      share|improve this answer























      • Not that it is a problem, but it's not exactly fast, is it? :)
        – Arnauld
        Aug 6 at 18:59










      • @Arnauld It uses roughly 3 integer partition functions in a single run :) Of course it is not too fast
        – Mr. Xcoder
        Aug 6 at 19:04










      • Now waiting to be outgolfed. I think it's possible in sub-35/30, but I don't think I'll be able to do anything much shorter
        – Mr. Xcoder
        Aug 6 at 19:15

















      up vote
      2
      down vote














      Jelly, 34 bytes



      BṚT’2*
      PÇŒṗæḟ2⁼ƊƇ€ŒpẎṢ⁼Ṣ}ʋƇÇ€×þ/ẎƊ


      Try it online!



      Input format: [P, Q] (the TIO link above doesn't accept this, but a single number instead, to aid with the test cases).



      Output format: List of all solutions (shown as grid representation of 3D list over TIO).



      Speed: Turtle.






      share|improve this answer




























        up vote
        1
        down vote














        Pyth, 27 bytes



        Inspired by Jonathan Allan's crushing of my Jelly solution. Takes $N$ as input.



        L^2x1jb2;hfqyQsMT./S*M*FyMP


        Try it here!






        share|improve this answer






























          up vote
          1
          down vote













          Haskell, 281 195 bytes



          import Data.List
          r=reverse.sort
          n#0=
          n#x=[id,(n:)]!!mod x 2$(n*2)#div x 2
          m!0=
          m!x=m!!0:tail m!(x-m!!0)
          m%=
          m%n=m!head n:drop(length$m!head n)m%tail n
          p&q=r[x*y|x<-1#p,y<-1#q]%r(1#(p*q))





          share|improve this answer



















          • 1




            Here are some tips: Defining operators instead of binary functions is cheaper, rearranging guards and pattern-matching can save you (==), use 1>0 instead of True and don't use where. Also n' can be shortened.. With this you can save 72 bytes: Try it online!
            – BWO
            Aug 7 at 21:21










          • Btw. you should check out the Haskell tips section if you havent.
            – BWO
            Aug 7 at 21:22










          • I had a look at the guard situation again, another 13 bytes off: Try it online!
            – BWO
            Aug 7 at 23:15











          • @OMᗺ, thank you. I'm new to haskell, so this looks for me as magic tricks
            – Ð•Ð²Ð³ÐµÐ½Ð¸Ð¹ Новиков
            Aug 8 at 7:01










          • No worries :) In case you have questions, feel free to ask in Of Monads and Men.
            – BWO
            Aug 8 at 12:15










          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%2f170077%2fsplit-the-bits%23new-answer', 'question_page');

          );

          Post as a guest






























          7 Answers
          7






          active

          oldest

          votes








          7 Answers
          7






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          4
          down vote














          K (ngn/k), 66 63 bytes



          a)?+b)_b:b@>b:,/*/:/2#a:*/'(&'x,*/x


          Try it online!



          accepts (P;Q) instead of N



          algorithm:



          • compute A as the partial sums of V(P*Q)


          • multiply each V(P) with each V(Q), sort the products in descending order (let's call that R), and compute their partial sums B


          • find the positions of those elements in B that also occur in A; cut R right after those positions






          share|improve this answer



























            up vote
            4
            down vote














            K (ngn/k), 66 63 bytes



            a)?+b)_b:b@>b:,/*/:/2#a:*/'(&'x,*/x


            Try it online!



            accepts (P;Q) instead of N



            algorithm:



            • compute A as the partial sums of V(P*Q)


            • multiply each V(P) with each V(Q), sort the products in descending order (let's call that R), and compute their partial sums B


            • find the positions of those elements in B that also occur in A; cut R right after those positions






            share|improve this answer

























              up vote
              4
              down vote










              up vote
              4
              down vote










              K (ngn/k), 66 63 bytes



              a)?+b)_b:b@>b:,/*/:/2#a:*/'(&'x,*/x


              Try it online!



              accepts (P;Q) instead of N



              algorithm:



              • compute A as the partial sums of V(P*Q)


              • multiply each V(P) with each V(Q), sort the products in descending order (let's call that R), and compute their partial sums B


              • find the positions of those elements in B that also occur in A; cut R right after those positions






              share|improve this answer
















              K (ngn/k), 66 63 bytes



              a)?+b)_b:b@>b:,/*/:/2#a:*/'(&'x,*/x


              Try it online!



              accepts (P;Q) instead of N



              algorithm:



              • compute A as the partial sums of V(P*Q)


              • multiply each V(P) with each V(Q), sort the products in descending order (let's call that R), and compute their partial sums B


              • find the positions of those elements in B that also occur in A; cut R right after those positions







              share|improve this answer















              share|improve this answer



              share|improve this answer








              edited Aug 6 at 18:39


























              answered Aug 6 at 15:51









              ngn

              6,59312154




              6,59312154




















                  up vote
                  3
                  down vote














                  Jelly, 24 bytes



                  BṚT’2*
                  Ç€×þ/FṢŒṖ§⁼Ç}ɗƇPḢ


                  A monadic link accepting a list of two integers [P, Q] which yields one possible list of lists as described in the question.



                  Try it online! (footer prints a string representation to show the list as it really is)



                  Or see the test-suite (taking a list of N and reordering results to be like those in the question)



                  How?



                  We may always slice up the elements of $M$ from lowest up, greedily (either there is a $1$ in $M$ or we had an input of $4$, when $M=[[4]]$) in order to find a solution.



                  Note: the code collects all (one!) such solutions in a list and then takes the head (only) result - i.e. the final head is necessary as the partitions are not of all possible orderings.



                  BṚT’2* - Link 1, powers of 2 that sum to N: integer, N e.g. 105
                  B - binary [1,1,0,1,0,0,1]
                  Ṛ - reverse [1,0,0,1,0,1,1]
                  T - truthy indices [1,4,6,7]
                  ’ - decrement [0,3,5,6]
                  2 - literal two 2
                  * - exponentiate [1,8,32,64]

                  Ç€×þ/FṢŒṖ§⁼Ç}ɗƇPḢ - Main Link: list of two integers, [P,Q]
                  Ç€ - call last Link (1) as a monad for €ach
                  / - reduce with:
                  þ - table with:
                  × - multiplication
                  F - flatten
                  á¹¢ - sort
                  ŒṖ - all partitions
                  Ƈ - filter keep if:
                  ɗ - last three links as a dyad:
                  § - sum each
                  } - use right...
                  P - ...value: product (i.e. P×Q)
                  Ç - ...do: call last Link (1) as a monad
                  ⁼ - equal? (non-vectorising so "all equal?")
                  Ḣ - head





                  share|improve this answer

























                    up vote
                    3
                    down vote














                    Jelly, 24 bytes



                    BṚT’2*
                    Ç€×þ/FṢŒṖ§⁼Ç}ɗƇPḢ


                    A monadic link accepting a list of two integers [P, Q] which yields one possible list of lists as described in the question.



                    Try it online! (footer prints a string representation to show the list as it really is)



                    Or see the test-suite (taking a list of N and reordering results to be like those in the question)



                    How?



                    We may always slice up the elements of $M$ from lowest up, greedily (either there is a $1$ in $M$ or we had an input of $4$, when $M=[[4]]$) in order to find a solution.



                    Note: the code collects all (one!) such solutions in a list and then takes the head (only) result - i.e. the final head is necessary as the partitions are not of all possible orderings.



                    BṚT’2* - Link 1, powers of 2 that sum to N: integer, N e.g. 105
                    B - binary [1,1,0,1,0,0,1]
                    Ṛ - reverse [1,0,0,1,0,1,1]
                    T - truthy indices [1,4,6,7]
                    ’ - decrement [0,3,5,6]
                    2 - literal two 2
                    * - exponentiate [1,8,32,64]

                    Ç€×þ/FṢŒṖ§⁼Ç}ɗƇPḢ - Main Link: list of two integers, [P,Q]
                    Ç€ - call last Link (1) as a monad for €ach
                    / - reduce with:
                    þ - table with:
                    × - multiplication
                    F - flatten
                    á¹¢ - sort
                    ŒṖ - all partitions
                    Ƈ - filter keep if:
                    ɗ - last three links as a dyad:
                    § - sum each
                    } - use right...
                    P - ...value: product (i.e. P×Q)
                    Ç - ...do: call last Link (1) as a monad
                    ⁼ - equal? (non-vectorising so "all equal?")
                    Ḣ - head





                    share|improve this answer























                      up vote
                      3
                      down vote










                      up vote
                      3
                      down vote










                      Jelly, 24 bytes



                      BṚT’2*
                      Ç€×þ/FṢŒṖ§⁼Ç}ɗƇPḢ


                      A monadic link accepting a list of two integers [P, Q] which yields one possible list of lists as described in the question.



                      Try it online! (footer prints a string representation to show the list as it really is)



                      Or see the test-suite (taking a list of N and reordering results to be like those in the question)



                      How?



                      We may always slice up the elements of $M$ from lowest up, greedily (either there is a $1$ in $M$ or we had an input of $4$, when $M=[[4]]$) in order to find a solution.



                      Note: the code collects all (one!) such solutions in a list and then takes the head (only) result - i.e. the final head is necessary as the partitions are not of all possible orderings.



                      BṚT’2* - Link 1, powers of 2 that sum to N: integer, N e.g. 105
                      B - binary [1,1,0,1,0,0,1]
                      Ṛ - reverse [1,0,0,1,0,1,1]
                      T - truthy indices [1,4,6,7]
                      ’ - decrement [0,3,5,6]
                      2 - literal two 2
                      * - exponentiate [1,8,32,64]

                      Ç€×þ/FṢŒṖ§⁼Ç}ɗƇPḢ - Main Link: list of two integers, [P,Q]
                      Ç€ - call last Link (1) as a monad for €ach
                      / - reduce with:
                      þ - table with:
                      × - multiplication
                      F - flatten
                      á¹¢ - sort
                      ŒṖ - all partitions
                      Ƈ - filter keep if:
                      ɗ - last three links as a dyad:
                      § - sum each
                      } - use right...
                      P - ...value: product (i.e. P×Q)
                      Ç - ...do: call last Link (1) as a monad
                      ⁼ - equal? (non-vectorising so "all equal?")
                      Ḣ - head





                      share|improve this answer














                      Jelly, 24 bytes



                      BṚT’2*
                      Ç€×þ/FṢŒṖ§⁼Ç}ɗƇPḢ


                      A monadic link accepting a list of two integers [P, Q] which yields one possible list of lists as described in the question.



                      Try it online! (footer prints a string representation to show the list as it really is)



                      Or see the test-suite (taking a list of N and reordering results to be like those in the question)



                      How?



                      We may always slice up the elements of $M$ from lowest up, greedily (either there is a $1$ in $M$ or we had an input of $4$, when $M=[[4]]$) in order to find a solution.



                      Note: the code collects all (one!) such solutions in a list and then takes the head (only) result - i.e. the final head is necessary as the partitions are not of all possible orderings.



                      BṚT’2* - Link 1, powers of 2 that sum to N: integer, N e.g. 105
                      B - binary [1,1,0,1,0,0,1]
                      Ṛ - reverse [1,0,0,1,0,1,1]
                      T - truthy indices [1,4,6,7]
                      ’ - decrement [0,3,5,6]
                      2 - literal two 2
                      * - exponentiate [1,8,32,64]

                      Ç€×þ/FṢŒṖ§⁼Ç}ɗƇPḢ - Main Link: list of two integers, [P,Q]
                      Ç€ - call last Link (1) as a monad for €ach
                      / - reduce with:
                      þ - table with:
                      × - multiplication
                      F - flatten
                      á¹¢ - sort
                      ŒṖ - all partitions
                      Ƈ - filter keep if:
                      ɗ - last three links as a dyad:
                      § - sum each
                      } - use right...
                      P - ...value: product (i.e. P×Q)
                      Ç - ...do: call last Link (1) as a monad
                      ⁼ - equal? (non-vectorising so "all equal?")
                      Ḣ - head






                      share|improve this answer













                      share|improve this answer



                      share|improve this answer











                      answered Aug 7 at 0:28









                      Jonathan Allan

                      47k433157




                      47k433157




















                          up vote
                          3
                          down vote














                          Python 2, 261 233 232 231 bytes





                          g=lambda n,r=,i=1:n and g(n/2,[i]*(n&1)+r,i*2)or r
                          def f(p,q):
                          V=[[v]for v in g(p*q)];i=j=0
                          for m in sorted(-a*b for a in g(p)for b in g(q)):
                          v=V[i]
                          while-m<v[j]:v[j:j+1]=[v[j]/2]*2
                          i,j=[i+1,i,0,j+1][j+1<len(v)::2]
                          return V


                          Try it online!



                          1 byte from Jo King; and another 1 byte due to Kevin Cruijssen.



                          Takes as input p,q. Pursues the greedy algorithm.






                          share|improve this answer























                          • -k-1 can be ~k.
                            – Jonathan Frech
                            Aug 7 at 14:33










                          • The i,j assignment can be i,j=[i+1,i,0,j+1][j+1<len(v)::2] for -1 byte
                            – Jo King
                            Aug 8 at 7:20










                          • @Jo King: Hahaha! That is twisted!
                            – Chas Brown
                            Aug 8 at 7:24










                          • while v[j]>-m can be while-m<v[j]
                            – Kevin Cruijssen
                            Aug 8 at 7:29










                          • @Kevin Cruijssen: Yes, indeed. Thx!
                            – Chas Brown
                            Aug 8 at 7:32














                          up vote
                          3
                          down vote














                          Python 2, 261 233 232 231 bytes





                          g=lambda n,r=,i=1:n and g(n/2,[i]*(n&1)+r,i*2)or r
                          def f(p,q):
                          V=[[v]for v in g(p*q)];i=j=0
                          for m in sorted(-a*b for a in g(p)for b in g(q)):
                          v=V[i]
                          while-m<v[j]:v[j:j+1]=[v[j]/2]*2
                          i,j=[i+1,i,0,j+1][j+1<len(v)::2]
                          return V


                          Try it online!



                          1 byte from Jo King; and another 1 byte due to Kevin Cruijssen.



                          Takes as input p,q. Pursues the greedy algorithm.






                          share|improve this answer























                          • -k-1 can be ~k.
                            – Jonathan Frech
                            Aug 7 at 14:33










                          • The i,j assignment can be i,j=[i+1,i,0,j+1][j+1<len(v)::2] for -1 byte
                            – Jo King
                            Aug 8 at 7:20










                          • @Jo King: Hahaha! That is twisted!
                            – Chas Brown
                            Aug 8 at 7:24










                          • while v[j]>-m can be while-m<v[j]
                            – Kevin Cruijssen
                            Aug 8 at 7:29










                          • @Kevin Cruijssen: Yes, indeed. Thx!
                            – Chas Brown
                            Aug 8 at 7:32












                          up vote
                          3
                          down vote










                          up vote
                          3
                          down vote










                          Python 2, 261 233 232 231 bytes





                          g=lambda n,r=,i=1:n and g(n/2,[i]*(n&1)+r,i*2)or r
                          def f(p,q):
                          V=[[v]for v in g(p*q)];i=j=0
                          for m in sorted(-a*b for a in g(p)for b in g(q)):
                          v=V[i]
                          while-m<v[j]:v[j:j+1]=[v[j]/2]*2
                          i,j=[i+1,i,0,j+1][j+1<len(v)::2]
                          return V


                          Try it online!



                          1 byte from Jo King; and another 1 byte due to Kevin Cruijssen.



                          Takes as input p,q. Pursues the greedy algorithm.






                          share|improve this answer
















                          Python 2, 261 233 232 231 bytes





                          g=lambda n,r=,i=1:n and g(n/2,[i]*(n&1)+r,i*2)or r
                          def f(p,q):
                          V=[[v]for v in g(p*q)];i=j=0
                          for m in sorted(-a*b for a in g(p)for b in g(q)):
                          v=V[i]
                          while-m<v[j]:v[j:j+1]=[v[j]/2]*2
                          i,j=[i+1,i,0,j+1][j+1<len(v)::2]
                          return V


                          Try it online!



                          1 byte from Jo King; and another 1 byte due to Kevin Cruijssen.



                          Takes as input p,q. Pursues the greedy algorithm.







                          share|improve this answer















                          share|improve this answer



                          share|improve this answer








                          edited Aug 8 at 7:31


























                          answered Aug 7 at 2:19









                          Chas Brown

                          3,855317




                          3,855317











                          • -k-1 can be ~k.
                            – Jonathan Frech
                            Aug 7 at 14:33










                          • The i,j assignment can be i,j=[i+1,i,0,j+1][j+1<len(v)::2] for -1 byte
                            – Jo King
                            Aug 8 at 7:20










                          • @Jo King: Hahaha! That is twisted!
                            – Chas Brown
                            Aug 8 at 7:24










                          • while v[j]>-m can be while-m<v[j]
                            – Kevin Cruijssen
                            Aug 8 at 7:29










                          • @Kevin Cruijssen: Yes, indeed. Thx!
                            – Chas Brown
                            Aug 8 at 7:32
















                          • -k-1 can be ~k.
                            – Jonathan Frech
                            Aug 7 at 14:33










                          • The i,j assignment can be i,j=[i+1,i,0,j+1][j+1<len(v)::2] for -1 byte
                            – Jo King
                            Aug 8 at 7:20










                          • @Jo King: Hahaha! That is twisted!
                            – Chas Brown
                            Aug 8 at 7:24










                          • while v[j]>-m can be while-m<v[j]
                            – Kevin Cruijssen
                            Aug 8 at 7:29










                          • @Kevin Cruijssen: Yes, indeed. Thx!
                            – Chas Brown
                            Aug 8 at 7:32















                          -k-1 can be ~k.
                          – Jonathan Frech
                          Aug 7 at 14:33




                          -k-1 can be ~k.
                          – Jonathan Frech
                          Aug 7 at 14:33












                          The i,j assignment can be i,j=[i+1,i,0,j+1][j+1<len(v)::2] for -1 byte
                          – Jo King
                          Aug 8 at 7:20




                          The i,j assignment can be i,j=[i+1,i,0,j+1][j+1<len(v)::2] for -1 byte
                          – Jo King
                          Aug 8 at 7:20












                          @Jo King: Hahaha! That is twisted!
                          – Chas Brown
                          Aug 8 at 7:24




                          @Jo King: Hahaha! That is twisted!
                          – Chas Brown
                          Aug 8 at 7:24












                          while v[j]>-m can be while-m<v[j]
                          – Kevin Cruijssen
                          Aug 8 at 7:29




                          while v[j]>-m can be while-m<v[j]
                          – Kevin Cruijssen
                          Aug 8 at 7:29












                          @Kevin Cruijssen: Yes, indeed. Thx!
                          – Chas Brown
                          Aug 8 at 7:32




                          @Kevin Cruijssen: Yes, indeed. Thx!
                          – Chas Brown
                          Aug 8 at 7:32










                          up vote
                          2
                          down vote














                          Jelly, 41 bytes



                          Œṗl2ĊƑ$Ƈ
                          PÇIP$ƇṪÇ€Œpµ³ÇIP$ƇṪƊ€ŒpZPṢ⁼FṢ$µƇ


                          Try it online!



                          Should probably be much shorter (some parts feel very repetitive; especially ÇIP$Ƈ, but I don't know how to golf it). Explanation to come after further golfing. Returns all possible solutions in case multiple exist and takes input as $[P, Q]$.






                          share|improve this answer























                          • Not that it is a problem, but it's not exactly fast, is it? :)
                            – Arnauld
                            Aug 6 at 18:59










                          • @Arnauld It uses roughly 3 integer partition functions in a single run :) Of course it is not too fast
                            – Mr. Xcoder
                            Aug 6 at 19:04










                          • Now waiting to be outgolfed. I think it's possible in sub-35/30, but I don't think I'll be able to do anything much shorter
                            – Mr. Xcoder
                            Aug 6 at 19:15














                          up vote
                          2
                          down vote














                          Jelly, 41 bytes



                          Œṗl2ĊƑ$Ƈ
                          PÇIP$ƇṪÇ€Œpµ³ÇIP$ƇṪƊ€ŒpZPṢ⁼FṢ$µƇ


                          Try it online!



                          Should probably be much shorter (some parts feel very repetitive; especially ÇIP$Ƈ, but I don't know how to golf it). Explanation to come after further golfing. Returns all possible solutions in case multiple exist and takes input as $[P, Q]$.






                          share|improve this answer























                          • Not that it is a problem, but it's not exactly fast, is it? :)
                            – Arnauld
                            Aug 6 at 18:59










                          • @Arnauld It uses roughly 3 integer partition functions in a single run :) Of course it is not too fast
                            – Mr. Xcoder
                            Aug 6 at 19:04










                          • Now waiting to be outgolfed. I think it's possible in sub-35/30, but I don't think I'll be able to do anything much shorter
                            – Mr. Xcoder
                            Aug 6 at 19:15












                          up vote
                          2
                          down vote










                          up vote
                          2
                          down vote










                          Jelly, 41 bytes



                          Œṗl2ĊƑ$Ƈ
                          PÇIP$ƇṪÇ€Œpµ³ÇIP$ƇṪƊ€ŒpZPṢ⁼FṢ$µƇ


                          Try it online!



                          Should probably be much shorter (some parts feel very repetitive; especially ÇIP$Ƈ, but I don't know how to golf it). Explanation to come after further golfing. Returns all possible solutions in case multiple exist and takes input as $[P, Q]$.






                          share|improve this answer
















                          Jelly, 41 bytes



                          Œṗl2ĊƑ$Ƈ
                          PÇIP$ƇṪÇ€Œpµ³ÇIP$ƇṪƊ€ŒpZPṢ⁼FṢ$µƇ


                          Try it online!



                          Should probably be much shorter (some parts feel very repetitive; especially ÇIP$Ƈ, but I don't know how to golf it). Explanation to come after further golfing. Returns all possible solutions in case multiple exist and takes input as $[P, Q]$.







                          share|improve this answer















                          share|improve this answer



                          share|improve this answer








                          edited Aug 6 at 18:39


























                          answered Aug 6 at 18:31









                          Mr. Xcoder

                          29.4k756192




                          29.4k756192











                          • Not that it is a problem, but it's not exactly fast, is it? :)
                            – Arnauld
                            Aug 6 at 18:59










                          • @Arnauld It uses roughly 3 integer partition functions in a single run :) Of course it is not too fast
                            – Mr. Xcoder
                            Aug 6 at 19:04










                          • Now waiting to be outgolfed. I think it's possible in sub-35/30, but I don't think I'll be able to do anything much shorter
                            – Mr. Xcoder
                            Aug 6 at 19:15
















                          • Not that it is a problem, but it's not exactly fast, is it? :)
                            – Arnauld
                            Aug 6 at 18:59










                          • @Arnauld It uses roughly 3 integer partition functions in a single run :) Of course it is not too fast
                            – Mr. Xcoder
                            Aug 6 at 19:04










                          • Now waiting to be outgolfed. I think it's possible in sub-35/30, but I don't think I'll be able to do anything much shorter
                            – Mr. Xcoder
                            Aug 6 at 19:15















                          Not that it is a problem, but it's not exactly fast, is it? :)
                          – Arnauld
                          Aug 6 at 18:59




                          Not that it is a problem, but it's not exactly fast, is it? :)
                          – Arnauld
                          Aug 6 at 18:59












                          @Arnauld It uses roughly 3 integer partition functions in a single run :) Of course it is not too fast
                          – Mr. Xcoder
                          Aug 6 at 19:04




                          @Arnauld It uses roughly 3 integer partition functions in a single run :) Of course it is not too fast
                          – Mr. Xcoder
                          Aug 6 at 19:04












                          Now waiting to be outgolfed. I think it's possible in sub-35/30, but I don't think I'll be able to do anything much shorter
                          – Mr. Xcoder
                          Aug 6 at 19:15




                          Now waiting to be outgolfed. I think it's possible in sub-35/30, but I don't think I'll be able to do anything much shorter
                          – Mr. Xcoder
                          Aug 6 at 19:15










                          up vote
                          2
                          down vote














                          Jelly, 34 bytes



                          BṚT’2*
                          PÇŒṗæḟ2⁼ƊƇ€ŒpẎṢ⁼Ṣ}ʋƇÇ€×þ/ẎƊ


                          Try it online!



                          Input format: [P, Q] (the TIO link above doesn't accept this, but a single number instead, to aid with the test cases).



                          Output format: List of all solutions (shown as grid representation of 3D list over TIO).



                          Speed: Turtle.






                          share|improve this answer

























                            up vote
                            2
                            down vote














                            Jelly, 34 bytes



                            BṚT’2*
                            PÇŒṗæḟ2⁼ƊƇ€ŒpẎṢ⁼Ṣ}ʋƇÇ€×þ/ẎƊ


                            Try it online!



                            Input format: [P, Q] (the TIO link above doesn't accept this, but a single number instead, to aid with the test cases).



                            Output format: List of all solutions (shown as grid representation of 3D list over TIO).



                            Speed: Turtle.






                            share|improve this answer























                              up vote
                              2
                              down vote










                              up vote
                              2
                              down vote










                              Jelly, 34 bytes



                              BṚT’2*
                              PÇŒṗæḟ2⁼ƊƇ€ŒpẎṢ⁼Ṣ}ʋƇÇ€×þ/ẎƊ


                              Try it online!



                              Input format: [P, Q] (the TIO link above doesn't accept this, but a single number instead, to aid with the test cases).



                              Output format: List of all solutions (shown as grid representation of 3D list over TIO).



                              Speed: Turtle.






                              share|improve this answer














                              Jelly, 34 bytes



                              BṚT’2*
                              PÇŒṗæḟ2⁼ƊƇ€ŒpẎṢ⁼Ṣ}ʋƇÇ€×þ/ẎƊ


                              Try it online!



                              Input format: [P, Q] (the TIO link above doesn't accept this, but a single number instead, to aid with the test cases).



                              Output format: List of all solutions (shown as grid representation of 3D list over TIO).



                              Speed: Turtle.







                              share|improve this answer













                              share|improve this answer



                              share|improve this answer











                              answered Aug 6 at 19:17









                              Erik the Outgolfer

                              29.2k42697




                              29.2k42697




















                                  up vote
                                  1
                                  down vote














                                  Pyth, 27 bytes



                                  Inspired by Jonathan Allan's crushing of my Jelly solution. Takes $N$ as input.



                                  L^2x1jb2;hfqyQsMT./S*M*FyMP


                                  Try it here!






                                  share|improve this answer



























                                    up vote
                                    1
                                    down vote














                                    Pyth, 27 bytes



                                    Inspired by Jonathan Allan's crushing of my Jelly solution. Takes $N$ as input.



                                    L^2x1jb2;hfqyQsMT./S*M*FyMP


                                    Try it here!






                                    share|improve this answer

























                                      up vote
                                      1
                                      down vote










                                      up vote
                                      1
                                      down vote










                                      Pyth, 27 bytes



                                      Inspired by Jonathan Allan's crushing of my Jelly solution. Takes $N$ as input.



                                      L^2x1jb2;hfqyQsMT./S*M*FyMP


                                      Try it here!






                                      share|improve this answer
















                                      Pyth, 27 bytes



                                      Inspired by Jonathan Allan's crushing of my Jelly solution. Takes $N$ as input.



                                      L^2x1jb2;hfqyQsMT./S*M*FyMP


                                      Try it here!







                                      share|improve this answer















                                      share|improve this answer



                                      share|improve this answer








                                      edited Aug 7 at 14:21


























                                      answered Aug 7 at 14:11









                                      Mr. Xcoder

                                      29.4k756192




                                      29.4k756192




















                                          up vote
                                          1
                                          down vote













                                          Haskell, 281 195 bytes



                                          import Data.List
                                          r=reverse.sort
                                          n#0=
                                          n#x=[id,(n:)]!!mod x 2$(n*2)#div x 2
                                          m!0=
                                          m!x=m!!0:tail m!(x-m!!0)
                                          m%=
                                          m%n=m!head n:drop(length$m!head n)m%tail n
                                          p&q=r[x*y|x<-1#p,y<-1#q]%r(1#(p*q))





                                          share|improve this answer



















                                          • 1




                                            Here are some tips: Defining operators instead of binary functions is cheaper, rearranging guards and pattern-matching can save you (==), use 1>0 instead of True and don't use where. Also n' can be shortened.. With this you can save 72 bytes: Try it online!
                                            – BWO
                                            Aug 7 at 21:21










                                          • Btw. you should check out the Haskell tips section if you havent.
                                            – BWO
                                            Aug 7 at 21:22










                                          • I had a look at the guard situation again, another 13 bytes off: Try it online!
                                            – BWO
                                            Aug 7 at 23:15











                                          • @OMᗺ, thank you. I'm new to haskell, so this looks for me as magic tricks
                                            – Ð•Ð²Ð³ÐµÐ½Ð¸Ð¹ Новиков
                                            Aug 8 at 7:01










                                          • No worries :) In case you have questions, feel free to ask in Of Monads and Men.
                                            – BWO
                                            Aug 8 at 12:15














                                          up vote
                                          1
                                          down vote













                                          Haskell, 281 195 bytes



                                          import Data.List
                                          r=reverse.sort
                                          n#0=
                                          n#x=[id,(n:)]!!mod x 2$(n*2)#div x 2
                                          m!0=
                                          m!x=m!!0:tail m!(x-m!!0)
                                          m%=
                                          m%n=m!head n:drop(length$m!head n)m%tail n
                                          p&q=r[x*y|x<-1#p,y<-1#q]%r(1#(p*q))





                                          share|improve this answer



















                                          • 1




                                            Here are some tips: Defining operators instead of binary functions is cheaper, rearranging guards and pattern-matching can save you (==), use 1>0 instead of True and don't use where. Also n' can be shortened.. With this you can save 72 bytes: Try it online!
                                            – BWO
                                            Aug 7 at 21:21










                                          • Btw. you should check out the Haskell tips section if you havent.
                                            – BWO
                                            Aug 7 at 21:22










                                          • I had a look at the guard situation again, another 13 bytes off: Try it online!
                                            – BWO
                                            Aug 7 at 23:15











                                          • @OMᗺ, thank you. I'm new to haskell, so this looks for me as magic tricks
                                            – Ð•Ð²Ð³ÐµÐ½Ð¸Ð¹ Новиков
                                            Aug 8 at 7:01










                                          • No worries :) In case you have questions, feel free to ask in Of Monads and Men.
                                            – BWO
                                            Aug 8 at 12:15












                                          up vote
                                          1
                                          down vote










                                          up vote
                                          1
                                          down vote









                                          Haskell, 281 195 bytes



                                          import Data.List
                                          r=reverse.sort
                                          n#0=
                                          n#x=[id,(n:)]!!mod x 2$(n*2)#div x 2
                                          m!0=
                                          m!x=m!!0:tail m!(x-m!!0)
                                          m%=
                                          m%n=m!head n:drop(length$m!head n)m%tail n
                                          p&q=r[x*y|x<-1#p,y<-1#q]%r(1#(p*q))





                                          share|improve this answer















                                          Haskell, 281 195 bytes



                                          import Data.List
                                          r=reverse.sort
                                          n#0=
                                          n#x=[id,(n:)]!!mod x 2$(n*2)#div x 2
                                          m!0=
                                          m!x=m!!0:tail m!(x-m!!0)
                                          m%=
                                          m%n=m!head n:drop(length$m!head n)m%tail n
                                          p&q=r[x*y|x<-1#p,y<-1#q]%r(1#(p*q))






                                          share|improve this answer















                                          share|improve this answer



                                          share|improve this answer








                                          edited Aug 8 at 7:00


























                                          answered Aug 7 at 20:32









                                          Евгений Новиков

                                          822116




                                          822116







                                          • 1




                                            Here are some tips: Defining operators instead of binary functions is cheaper, rearranging guards and pattern-matching can save you (==), use 1>0 instead of True and don't use where. Also n' can be shortened.. With this you can save 72 bytes: Try it online!
                                            – BWO
                                            Aug 7 at 21:21










                                          • Btw. you should check out the Haskell tips section if you havent.
                                            – BWO
                                            Aug 7 at 21:22










                                          • I had a look at the guard situation again, another 13 bytes off: Try it online!
                                            – BWO
                                            Aug 7 at 23:15











                                          • @OMᗺ, thank you. I'm new to haskell, so this looks for me as magic tricks
                                            – Ð•Ð²Ð³ÐµÐ½Ð¸Ð¹ Новиков
                                            Aug 8 at 7:01










                                          • No worries :) In case you have questions, feel free to ask in Of Monads and Men.
                                            – BWO
                                            Aug 8 at 12:15












                                          • 1




                                            Here are some tips: Defining operators instead of binary functions is cheaper, rearranging guards and pattern-matching can save you (==), use 1>0 instead of True and don't use where. Also n' can be shortened.. With this you can save 72 bytes: Try it online!
                                            – BWO
                                            Aug 7 at 21:21










                                          • Btw. you should check out the Haskell tips section if you havent.
                                            – BWO
                                            Aug 7 at 21:22










                                          • I had a look at the guard situation again, another 13 bytes off: Try it online!
                                            – BWO
                                            Aug 7 at 23:15











                                          • @OMᗺ, thank you. I'm new to haskell, so this looks for me as magic tricks
                                            – Ð•Ð²Ð³ÐµÐ½Ð¸Ð¹ Новиков
                                            Aug 8 at 7:01










                                          • No worries :) In case you have questions, feel free to ask in Of Monads and Men.
                                            – BWO
                                            Aug 8 at 12:15







                                          1




                                          1




                                          Here are some tips: Defining operators instead of binary functions is cheaper, rearranging guards and pattern-matching can save you (==), use 1>0 instead of True and don't use where. Also n' can be shortened.. With this you can save 72 bytes: Try it online!
                                          – BWO
                                          Aug 7 at 21:21




                                          Here are some tips: Defining operators instead of binary functions is cheaper, rearranging guards and pattern-matching can save you (==), use 1>0 instead of True and don't use where. Also n' can be shortened.. With this you can save 72 bytes: Try it online!
                                          – BWO
                                          Aug 7 at 21:21












                                          Btw. you should check out the Haskell tips section if you havent.
                                          – BWO
                                          Aug 7 at 21:22




                                          Btw. you should check out the Haskell tips section if you havent.
                                          – BWO
                                          Aug 7 at 21:22












                                          I had a look at the guard situation again, another 13 bytes off: Try it online!
                                          – BWO
                                          Aug 7 at 23:15





                                          I had a look at the guard situation again, another 13 bytes off: Try it online!
                                          – BWO
                                          Aug 7 at 23:15













                                          @OMᗺ, thank you. I'm new to haskell, so this looks for me as magic tricks
                                          – Ð•Ð²Ð³ÐµÐ½Ð¸Ð¹ Новиков
                                          Aug 8 at 7:01




                                          @OMᗺ, thank you. I'm new to haskell, so this looks for me as magic tricks
                                          – Ð•Ð²Ð³ÐµÐ½Ð¸Ð¹ Новиков
                                          Aug 8 at 7:01












                                          No worries :) In case you have questions, feel free to ask in Of Monads and Men.
                                          – BWO
                                          Aug 8 at 12:15




                                          No worries :) In case you have questions, feel free to ask in Of Monads and Men.
                                          – BWO
                                          Aug 8 at 12:15












                                           

                                          draft saved


                                          draft discarded


























                                           


                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function ()
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f170077%2fsplit-the-bits%23new-answer', 'question_page');

                                          );

                                          Post as a guest













































































                                          Comments

                                          Popular posts from this blog

                                          Color the edges and diagonals of a regular polygon

                                          Relationship between determinant of matrix and determinant of adjoint?

                                          What is the equation of a 3D cone with generalised tilt?