Fermat's polygonal number theoremHardy–Ramanujan number generalizationCounting +1 primesPentagonal numbers made from pentagonal numbersDecompose binary into alternating subsequencesGoldbach partitionsPolygonal Numbers!Is this number triangular?Yo boy, must it sumIs it a whole number?Find the percentage

'Pound' meaning in this context

How to give a rationality-inducing drug to an entire software company?

3-prong to 4-prong conversion - EXTRA MISLABELLED WIRES - Dryer cable upgrade and installation

Code Golf Measurer © 2019

Why does b+=(4,) work and b = b + (4,) doesn't work when b is a list?

Can you upgrade armour from breastplate to halfplate?

How to see time in ~/.bash_history file

How can a "proper" function have a vertical slope?

My Villain scrys on the party, but I forgot about the sensor!

In the comics, have any of the Robins called their costume "target attraction" for villains?

What is "demographic engineering" and how does it differ from ethnic cleansing?

Why do adjectives come before nouns in English?

I got this nail stuck in my tire, should I plug or replace?

Which culture used no personal names?

"A tin of biscuits" vs "A biscuit tin"

Can we not simply connect a battery to a RAM to prevent data loss during power cuts?

What are the branches of statistics?

Function of 令 in this sentence (cantonese)

What does "stirring tanks" mean?

Why does Sonny say they call Michael "Giogali"?

Do businesses save their customers' credit card information until the payment is finalized?

I am often given, occasionally stolen, rarely sold, and never borrowed

How to plot two axis bar chart side by side

What is the difference between a ciaccona (chaconne) and a passacaglia?



Fermat's polygonal number theorem


Hardy–Ramanujan number generalizationCounting +1 primesPentagonal numbers made from pentagonal numbersDecompose binary into alternating subsequencesGoldbach partitionsPolygonal Numbers!Is this number triangular?Yo boy, must it sumIs it a whole number?Find the percentage






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;









10














$begingroup$


Fermat's polygonal number theorem states that every positive integer can be expressed as the sum of at most $n$ $n$-gonal numbers. This means that every positive integer can be expressed as the sum of up to three triangle numbers, four square numbers, five pentagonal numbers etc. Your task is to take a positive integer $x$, and an integer $s ge 3$, and to output the $s$-gonal integers which sum to $x$.



The $n$-th $s$-gonal integer, where $n ge 1$ and $s ge 3$, can be defined in a couple of ways. The non-math-y way is that the $n$th $s$-gonal number can be constructed as a regular polygon with $s$ sides, each of length $n$. For example, for $s = 3$ (triangular numbers):



triangles



See here for examples with a larger $s$.



The math-y definition is by using the formula for $P(n, s)$, which yields the $n$-th $s$-gonal number:



$$P(n, s) = fracn^2(s-2) - n(s-4)2$$



which is given in the Wikipedia page here.



Input



Two positive integers, $s$ and $x$, with the condition $s ge 3$. You may input these integers in the most natural representation in your language (decimal, unary, Church numerals, integer-valued floating point numbers etc.).



Output



A list of integers, $L$, with a maximum length of $s$, where the sum of $L$ is equal to $x$ and all integers in $L$ are $s$-gonal integers. Again, the integers may be outputted in the natural representation in your language, with any distinct, consistent separator (so non-decimal character(s) for decimal output, a character different from that used for unary output etc.)



Rules



  • The inputs or outputs will never exceed the integer limit for your language


  • $L$ does not have to be ordered

  • In case of multiple possible outputs, any or all are acceptable

  • This is code-golf so the shortest code in bytes wins

Test cases



 x, s => L
1, s => 1
2, s => 1, 1
5, 6 => 1, 1, 1, 1, 1
17, 3 => 1, 6, 10
17, 4 => 1, 16
17, 5 => 5, 12
36, 3 => 36
43, 6 => 15, 28
879, 17 => 17, 48, 155, 231, 428
4856, 23 => 130, 448, 955, 1398, 1925









share|improve this question









$endgroup$















  • $begingroup$
    Sandbox post
    $endgroup$
    – caird coinheringaahing
    8 hours ago










  • $begingroup$
    Can the output have some zero padding? For instance if we consider x=17, s=5 could we output 5,12,0,0,0 instead of just 5,12?
    $endgroup$
    – flawr
    8 hours ago










  • $begingroup$
    @flawr So long as the length of the array doesn't exceed $s$, even with padding, that's fine
    $endgroup$
    – caird coinheringaahing
    8 hours ago










  • $begingroup$
    Are repeats allowed or should I add a Q to my submission?
    $endgroup$
    – Jonathan Allan
    7 hours ago










  • $begingroup$
    @JonathanAllan Repeated outputs are perfectly fine (if outputting multiple solutions)
    $endgroup$
    – caird coinheringaahing
    7 hours ago

















10














$begingroup$


Fermat's polygonal number theorem states that every positive integer can be expressed as the sum of at most $n$ $n$-gonal numbers. This means that every positive integer can be expressed as the sum of up to three triangle numbers, four square numbers, five pentagonal numbers etc. Your task is to take a positive integer $x$, and an integer $s ge 3$, and to output the $s$-gonal integers which sum to $x$.



The $n$-th $s$-gonal integer, where $n ge 1$ and $s ge 3$, can be defined in a couple of ways. The non-math-y way is that the $n$th $s$-gonal number can be constructed as a regular polygon with $s$ sides, each of length $n$. For example, for $s = 3$ (triangular numbers):



triangles



See here for examples with a larger $s$.



The math-y definition is by using the formula for $P(n, s)$, which yields the $n$-th $s$-gonal number:



$$P(n, s) = fracn^2(s-2) - n(s-4)2$$



which is given in the Wikipedia page here.



Input



Two positive integers, $s$ and $x$, with the condition $s ge 3$. You may input these integers in the most natural representation in your language (decimal, unary, Church numerals, integer-valued floating point numbers etc.).



Output



A list of integers, $L$, with a maximum length of $s$, where the sum of $L$ is equal to $x$ and all integers in $L$ are $s$-gonal integers. Again, the integers may be outputted in the natural representation in your language, with any distinct, consistent separator (so non-decimal character(s) for decimal output, a character different from that used for unary output etc.)



Rules



  • The inputs or outputs will never exceed the integer limit for your language


  • $L$ does not have to be ordered

  • In case of multiple possible outputs, any or all are acceptable

  • This is code-golf so the shortest code in bytes wins

Test cases



 x, s => L
1, s => 1
2, s => 1, 1
5, 6 => 1, 1, 1, 1, 1
17, 3 => 1, 6, 10
17, 4 => 1, 16
17, 5 => 5, 12
36, 3 => 36
43, 6 => 15, 28
879, 17 => 17, 48, 155, 231, 428
4856, 23 => 130, 448, 955, 1398, 1925









share|improve this question









$endgroup$















  • $begingroup$
    Sandbox post
    $endgroup$
    – caird coinheringaahing
    8 hours ago










  • $begingroup$
    Can the output have some zero padding? For instance if we consider x=17, s=5 could we output 5,12,0,0,0 instead of just 5,12?
    $endgroup$
    – flawr
    8 hours ago










  • $begingroup$
    @flawr So long as the length of the array doesn't exceed $s$, even with padding, that's fine
    $endgroup$
    – caird coinheringaahing
    8 hours ago










  • $begingroup$
    Are repeats allowed or should I add a Q to my submission?
    $endgroup$
    – Jonathan Allan
    7 hours ago










  • $begingroup$
    @JonathanAllan Repeated outputs are perfectly fine (if outputting multiple solutions)
    $endgroup$
    – caird coinheringaahing
    7 hours ago













10












10








10





$begingroup$


Fermat's polygonal number theorem states that every positive integer can be expressed as the sum of at most $n$ $n$-gonal numbers. This means that every positive integer can be expressed as the sum of up to three triangle numbers, four square numbers, five pentagonal numbers etc. Your task is to take a positive integer $x$, and an integer $s ge 3$, and to output the $s$-gonal integers which sum to $x$.



The $n$-th $s$-gonal integer, where $n ge 1$ and $s ge 3$, can be defined in a couple of ways. The non-math-y way is that the $n$th $s$-gonal number can be constructed as a regular polygon with $s$ sides, each of length $n$. For example, for $s = 3$ (triangular numbers):



triangles



See here for examples with a larger $s$.



The math-y definition is by using the formula for $P(n, s)$, which yields the $n$-th $s$-gonal number:



$$P(n, s) = fracn^2(s-2) - n(s-4)2$$



which is given in the Wikipedia page here.



Input



Two positive integers, $s$ and $x$, with the condition $s ge 3$. You may input these integers in the most natural representation in your language (decimal, unary, Church numerals, integer-valued floating point numbers etc.).



Output



A list of integers, $L$, with a maximum length of $s$, where the sum of $L$ is equal to $x$ and all integers in $L$ are $s$-gonal integers. Again, the integers may be outputted in the natural representation in your language, with any distinct, consistent separator (so non-decimal character(s) for decimal output, a character different from that used for unary output etc.)



Rules



  • The inputs or outputs will never exceed the integer limit for your language


  • $L$ does not have to be ordered

  • In case of multiple possible outputs, any or all are acceptable

  • This is code-golf so the shortest code in bytes wins

Test cases



 x, s => L
1, s => 1
2, s => 1, 1
5, 6 => 1, 1, 1, 1, 1
17, 3 => 1, 6, 10
17, 4 => 1, 16
17, 5 => 5, 12
36, 3 => 36
43, 6 => 15, 28
879, 17 => 17, 48, 155, 231, 428
4856, 23 => 130, 448, 955, 1398, 1925









share|improve this question









$endgroup$




Fermat's polygonal number theorem states that every positive integer can be expressed as the sum of at most $n$ $n$-gonal numbers. This means that every positive integer can be expressed as the sum of up to three triangle numbers, four square numbers, five pentagonal numbers etc. Your task is to take a positive integer $x$, and an integer $s ge 3$, and to output the $s$-gonal integers which sum to $x$.



The $n$-th $s$-gonal integer, where $n ge 1$ and $s ge 3$, can be defined in a couple of ways. The non-math-y way is that the $n$th $s$-gonal number can be constructed as a regular polygon with $s$ sides, each of length $n$. For example, for $s = 3$ (triangular numbers):



triangles



See here for examples with a larger $s$.



The math-y definition is by using the formula for $P(n, s)$, which yields the $n$-th $s$-gonal number:



$$P(n, s) = fracn^2(s-2) - n(s-4)2$$



which is given in the Wikipedia page here.



Input



Two positive integers, $s$ and $x$, with the condition $s ge 3$. You may input these integers in the most natural representation in your language (decimal, unary, Church numerals, integer-valued floating point numbers etc.).



Output



A list of integers, $L$, with a maximum length of $s$, where the sum of $L$ is equal to $x$ and all integers in $L$ are $s$-gonal integers. Again, the integers may be outputted in the natural representation in your language, with any distinct, consistent separator (so non-decimal character(s) for decimal output, a character different from that used for unary output etc.)



Rules



  • The inputs or outputs will never exceed the integer limit for your language


  • $L$ does not have to be ordered

  • In case of multiple possible outputs, any or all are acceptable

  • This is code-golf so the shortest code in bytes wins

Test cases



 x, s => L
1, s => 1
2, s => 1, 1
5, 6 => 1, 1, 1, 1, 1
17, 3 => 1, 6, 10
17, 4 => 1, 16
17, 5 => 5, 12
36, 3 => 36
43, 6 => 15, 28
879, 17 => 17, 48, 155, 231, 428
4856, 23 => 130, 448, 955, 1398, 1925






code-golf math sequence number-theory






share|improve this question













share|improve this question











share|improve this question




share|improve this question



share|improve this question










asked 8 hours ago









caird coinheringaahingcaird coinheringaahing

8,2253 gold badges32 silver badges94 bronze badges




8,2253 gold badges32 silver badges94 bronze badges














  • $begingroup$
    Sandbox post
    $endgroup$
    – caird coinheringaahing
    8 hours ago










  • $begingroup$
    Can the output have some zero padding? For instance if we consider x=17, s=5 could we output 5,12,0,0,0 instead of just 5,12?
    $endgroup$
    – flawr
    8 hours ago










  • $begingroup$
    @flawr So long as the length of the array doesn't exceed $s$, even with padding, that's fine
    $endgroup$
    – caird coinheringaahing
    8 hours ago










  • $begingroup$
    Are repeats allowed or should I add a Q to my submission?
    $endgroup$
    – Jonathan Allan
    7 hours ago










  • $begingroup$
    @JonathanAllan Repeated outputs are perfectly fine (if outputting multiple solutions)
    $endgroup$
    – caird coinheringaahing
    7 hours ago
















  • $begingroup$
    Sandbox post
    $endgroup$
    – caird coinheringaahing
    8 hours ago










  • $begingroup$
    Can the output have some zero padding? For instance if we consider x=17, s=5 could we output 5,12,0,0,0 instead of just 5,12?
    $endgroup$
    – flawr
    8 hours ago










  • $begingroup$
    @flawr So long as the length of the array doesn't exceed $s$, even with padding, that's fine
    $endgroup$
    – caird coinheringaahing
    8 hours ago










  • $begingroup$
    Are repeats allowed or should I add a Q to my submission?
    $endgroup$
    – Jonathan Allan
    7 hours ago










  • $begingroup$
    @JonathanAllan Repeated outputs are perfectly fine (if outputting multiple solutions)
    $endgroup$
    – caird coinheringaahing
    7 hours ago















$begingroup$
Sandbox post
$endgroup$
– caird coinheringaahing
8 hours ago




$begingroup$
Sandbox post
$endgroup$
– caird coinheringaahing
8 hours ago












$begingroup$
Can the output have some zero padding? For instance if we consider x=17, s=5 could we output 5,12,0,0,0 instead of just 5,12?
$endgroup$
– flawr
8 hours ago




$begingroup$
Can the output have some zero padding? For instance if we consider x=17, s=5 could we output 5,12,0,0,0 instead of just 5,12?
$endgroup$
– flawr
8 hours ago












$begingroup$
@flawr So long as the length of the array doesn't exceed $s$, even with padding, that's fine
$endgroup$
– caird coinheringaahing
8 hours ago




$begingroup$
@flawr So long as the length of the array doesn't exceed $s$, even with padding, that's fine
$endgroup$
– caird coinheringaahing
8 hours ago












$begingroup$
Are repeats allowed or should I add a Q to my submission?
$endgroup$
– Jonathan Allan
7 hours ago




$begingroup$
Are repeats allowed or should I add a Q to my submission?
$endgroup$
– Jonathan Allan
7 hours ago












$begingroup$
@JonathanAllan Repeated outputs are perfectly fine (if outputting multiple solutions)
$endgroup$
– caird coinheringaahing
7 hours ago




$begingroup$
@JonathanAllan Repeated outputs are perfectly fine (if outputting multiple solutions)
$endgroup$
– caird coinheringaahing
7 hours ago










5 Answers
5






active

oldest

votes


















2
















$begingroup$


Haskell, 78 80 77 bytes



We compute the cartesian product of the first $n$ s-gonal numbers, and then find the first entry that sums to $n$.





s#n=[x|x<-mapM(map(n->s*(n^2-n)`div`2+n*(2-n)))([0..n]<$[1..s]),sum x==n]!!0


Try it online!






share|improve this answer












$endgroup$










  • 1




    $begingroup$
    This does not work for 2, s or 5, 6 etc (multiplicities needed)
    $endgroup$
    – Jonathan Allan
    7 hours ago










  • $begingroup$
    @JonathanAllan I fixed it now - I misunderstood something. (It even saved me one byte:)
    $endgroup$
    – flawr
    7 hours ago



















1
















$begingroup$


Jelly, 17 bytes



x’2;’ÄÄx⁸ŒPS⁼¥Ƈ⁹Ḣ


A (very very inefficient) dyadic Link accepting s on the left and x on the right which yields the shortest possible answer as a list of integers (sorted ascending).



Try it online! - not much point trying it for much higher values!



How?



x’2;’ÄÄx⁸ŒPS⁼¥Ƈ⁹Ḣ - Link: s, x e.g. 5, 17
x - repeat (s) (x) times [5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5]
’ - decrement (vectorises) [4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]
2; - prepend a two [2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]
’ - decrement (vectorises) [1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
Ä - cumulative sums [1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34, 37, 40, 43, 46, 49, 52]
Ä - cumulative sums [1, 5, 12, 22, 35, 51, 70, 92, 117, 145, 176, 210, 247, 287, 330, 376, 425, 477]
x⁸ - repeat (each of those) (s) times [1, 1, 1, 5, ..., 425, 477, 477, 477]
ŒP - power-set [[], [1], [1], ..., [1, 1], ..., [5, 22, 70], ... etc]
(this has 2^(x(s+1)) entries ...this example would have 2^(17(5+1)) = 2^102 = 5070602400912917605986812821504 entries!)
(Note: the lengths increase left to right)
Ƈ - filter keep if:
¥ - last two links as a dyad:
S - sum
⁼ ⁹ - equals (x)? [[5,12], ... , [5,12], [1, 1, 5, 5, 5], ... , [1, 1, 5, 5, 5], [1, 1, 1, 1, 1, 12], ...]
Ḣ - head [5,12]





share|improve this answer












$endgroup$






















    1
















    $begingroup$


    Ruby, 79 bytes



    Computes the first $n$ $s$-gonal numbers and zero, gets the cartesian product of itself $s$ times, and finds the first entry that matches. Later test cases run out of memory on TIO but theoretically work.



    $fracn^2(s-2)-n(s-4)2$ is reorganized into $fracn(ns-2n-s+4)2$ because it saves bytes in Ruby.





    ->n,sa=(0..n).mapi*(i*s-2*i-s+4)/2;a.product(*[a]*~-s).finda


    Try it online!






    share|improve this answer










    $endgroup$






















      1
















      $begingroup$

      JavaScript (ES6),  83  80 bytes



      Takes input as (s)(x).





      s=>g=(x,n=0,a=[],y=~n*(~-n-n*s/2))=>x<y?x|a[s]?0:a:g(x,n+1,a)||g(x-y,n,[...a,y])


      Try it online!



      Formula



      It turns out to be shorter to start with $n=0$ and to compute $P(n+1,s)$:



      $$beginalignP(n+1,s)&=((n+1)^2(s-2)-(n+1)(s-4))/2\
      &=(n^2(s-2)+ns+2)/2\
      &=-(n+1)((n-1)-ns/2)
      endalign$$



      which can be written in 14 bytes in JS:



      ~n*(~-n-n*s/2)


      Commented



      s => // main function taking s
      g = ( // recursive function g
      x, // taking x
      n = 0, // start with n = 0
      a = [], // a[] = list of s-gonal numbers
      y = // y = P(n + 1, s)
      ~n * (~-n - n * s / 2) // = -(n + 1) * ((n - 1) - n * s / 2)
      ) => //
      x < y ? // if x is less than P(n + 1, s):
      x | a[s] ? // if x is not equal to 0 or a[] is too long:
      0 // failed: return 0
      : // else:
      a // success: return a[]
      : // else:
      // recursive calls:
      g(x, n + 1, a) || // preferred: try to increment n
      g(x - y, n, [...a, y]) // fallback : try to use the current s-gonal number





      share|improve this answer












      $endgroup$






















        0
















        $begingroup$


        Retina, 111 bytes



        d+
        *
        ~(`$
        $"
        0%["^_+ "|""]'$L$`G_(?<=(?=___(_*))_+)
        ((_(?($.(2*$>`))$1$.(2*$>`)))$*)
        1%|' L$`G_
        $$.$.($`$>`


        Try it online! Link includes test cases. Takes input in the order s n. Explanation:



        d+
        *


        Convert to unary.



        ~(`


        After processing the remaining stages, treat them as a Retina program and execute them on the same input.



        $
        $"


        Duplicate the line.



        0%["^_+ "|""]'$L$`G_(?<=(?=___(_*))_+)
        ((_(?($.(2*$>`))$1$.(2*$>`)))$*)


        Replace the first copy with a regular expression that skips over the first number and then matches s s-gonal numbers. The numbers themselves are captured in odd capture groups and the even capture groups are used to ensure that all of the numbers are s-gonal.



        1%|' L$`G_
        $$.$.($`$>`


        Replace the second copy with a space-separated list of the odd capture groups.



        As an example, the generated code for an input of 5 17 is as follows:



        ^_+ ((_(?(2)__2))*)((_(?(4)__4))*)((_(?(6)__6))*)((_(?(8)__8))*)((_(?(10)__10))*)$
        $.1 $.3 $.5 $.7 $.9





        share|improve this answer










        $endgroup$
















          Your Answer






          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',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          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%2f194042%2ffermats-polygonal-number-theorem%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown


























          5 Answers
          5






          active

          oldest

          votes








          5 Answers
          5






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2
















          $begingroup$


          Haskell, 78 80 77 bytes



          We compute the cartesian product of the first $n$ s-gonal numbers, and then find the first entry that sums to $n$.





          s#n=[x|x<-mapM(map(n->s*(n^2-n)`div`2+n*(2-n)))([0..n]<$[1..s]),sum x==n]!!0


          Try it online!






          share|improve this answer












          $endgroup$










          • 1




            $begingroup$
            This does not work for 2, s or 5, 6 etc (multiplicities needed)
            $endgroup$
            – Jonathan Allan
            7 hours ago










          • $begingroup$
            @JonathanAllan I fixed it now - I misunderstood something. (It even saved me one byte:)
            $endgroup$
            – flawr
            7 hours ago
















          2
















          $begingroup$


          Haskell, 78 80 77 bytes



          We compute the cartesian product of the first $n$ s-gonal numbers, and then find the first entry that sums to $n$.





          s#n=[x|x<-mapM(map(n->s*(n^2-n)`div`2+n*(2-n)))([0..n]<$[1..s]),sum x==n]!!0


          Try it online!






          share|improve this answer












          $endgroup$










          • 1




            $begingroup$
            This does not work for 2, s or 5, 6 etc (multiplicities needed)
            $endgroup$
            – Jonathan Allan
            7 hours ago










          • $begingroup$
            @JonathanAllan I fixed it now - I misunderstood something. (It even saved me one byte:)
            $endgroup$
            – flawr
            7 hours ago














          2














          2










          2







          $begingroup$


          Haskell, 78 80 77 bytes



          We compute the cartesian product of the first $n$ s-gonal numbers, and then find the first entry that sums to $n$.





          s#n=[x|x<-mapM(map(n->s*(n^2-n)`div`2+n*(2-n)))([0..n]<$[1..s]),sum x==n]!!0


          Try it online!






          share|improve this answer












          $endgroup$




          Haskell, 78 80 77 bytes



          We compute the cartesian product of the first $n$ s-gonal numbers, and then find the first entry that sums to $n$.





          s#n=[x|x<-mapM(map(n->s*(n^2-n)`div`2+n*(2-n)))([0..n]<$[1..s]),sum x==n]!!0


          Try it online!







          share|improve this answer















          share|improve this answer




          share|improve this answer



          share|improve this answer








          edited 7 hours ago

























          answered 8 hours ago









          flawrflawr

          30.5k7 gold badges82 silver badges215 bronze badges




          30.5k7 gold badges82 silver badges215 bronze badges










          • 1




            $begingroup$
            This does not work for 2, s or 5, 6 etc (multiplicities needed)
            $endgroup$
            – Jonathan Allan
            7 hours ago










          • $begingroup$
            @JonathanAllan I fixed it now - I misunderstood something. (It even saved me one byte:)
            $endgroup$
            – flawr
            7 hours ago













          • 1




            $begingroup$
            This does not work for 2, s or 5, 6 etc (multiplicities needed)
            $endgroup$
            – Jonathan Allan
            7 hours ago










          • $begingroup$
            @JonathanAllan I fixed it now - I misunderstood something. (It even saved me one byte:)
            $endgroup$
            – flawr
            7 hours ago








          1




          1




          $begingroup$
          This does not work for 2, s or 5, 6 etc (multiplicities needed)
          $endgroup$
          – Jonathan Allan
          7 hours ago




          $begingroup$
          This does not work for 2, s or 5, 6 etc (multiplicities needed)
          $endgroup$
          – Jonathan Allan
          7 hours ago












          $begingroup$
          @JonathanAllan I fixed it now - I misunderstood something. (It even saved me one byte:)
          $endgroup$
          – flawr
          7 hours ago





          $begingroup$
          @JonathanAllan I fixed it now - I misunderstood something. (It even saved me one byte:)
          $endgroup$
          – flawr
          7 hours ago














          1
















          $begingroup$


          Jelly, 17 bytes



          x’2;’ÄÄx⁸ŒPS⁼¥Ƈ⁹Ḣ


          A (very very inefficient) dyadic Link accepting s on the left and x on the right which yields the shortest possible answer as a list of integers (sorted ascending).



          Try it online! - not much point trying it for much higher values!



          How?



          x’2;’ÄÄx⁸ŒPS⁼¥Ƈ⁹Ḣ - Link: s, x e.g. 5, 17
          x - repeat (s) (x) times [5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5]
          ’ - decrement (vectorises) [4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]
          2; - prepend a two [2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]
          ’ - decrement (vectorises) [1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
          Ä - cumulative sums [1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34, 37, 40, 43, 46, 49, 52]
          Ä - cumulative sums [1, 5, 12, 22, 35, 51, 70, 92, 117, 145, 176, 210, 247, 287, 330, 376, 425, 477]
          x⁸ - repeat (each of those) (s) times [1, 1, 1, 5, ..., 425, 477, 477, 477]
          ŒP - power-set [[], [1], [1], ..., [1, 1], ..., [5, 22, 70], ... etc]
          (this has 2^(x(s+1)) entries ...this example would have 2^(17(5+1)) = 2^102 = 5070602400912917605986812821504 entries!)
          (Note: the lengths increase left to right)
          Ƈ - filter keep if:
          ¥ - last two links as a dyad:
          S - sum
          ⁼ ⁹ - equals (x)? [[5,12], ... , [5,12], [1, 1, 5, 5, 5], ... , [1, 1, 5, 5, 5], [1, 1, 1, 1, 1, 12], ...]
          Ḣ - head [5,12]





          share|improve this answer












          $endgroup$



















            1
















            $begingroup$


            Jelly, 17 bytes



            x’2;’ÄÄx⁸ŒPS⁼¥Ƈ⁹Ḣ


            A (very very inefficient) dyadic Link accepting s on the left and x on the right which yields the shortest possible answer as a list of integers (sorted ascending).



            Try it online! - not much point trying it for much higher values!



            How?



            x’2;’ÄÄx⁸ŒPS⁼¥Ƈ⁹Ḣ - Link: s, x e.g. 5, 17
            x - repeat (s) (x) times [5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5]
            ’ - decrement (vectorises) [4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]
            2; - prepend a two [2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]
            ’ - decrement (vectorises) [1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
            Ä - cumulative sums [1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34, 37, 40, 43, 46, 49, 52]
            Ä - cumulative sums [1, 5, 12, 22, 35, 51, 70, 92, 117, 145, 176, 210, 247, 287, 330, 376, 425, 477]
            x⁸ - repeat (each of those) (s) times [1, 1, 1, 5, ..., 425, 477, 477, 477]
            ŒP - power-set [[], [1], [1], ..., [1, 1], ..., [5, 22, 70], ... etc]
            (this has 2^(x(s+1)) entries ...this example would have 2^(17(5+1)) = 2^102 = 5070602400912917605986812821504 entries!)
            (Note: the lengths increase left to right)
            Ƈ - filter keep if:
            ¥ - last two links as a dyad:
            S - sum
            ⁼ ⁹ - equals (x)? [[5,12], ... , [5,12], [1, 1, 5, 5, 5], ... , [1, 1, 5, 5, 5], [1, 1, 1, 1, 1, 12], ...]
            Ḣ - head [5,12]





            share|improve this answer












            $endgroup$

















              1














              1










              1







              $begingroup$


              Jelly, 17 bytes



              x’2;’ÄÄx⁸ŒPS⁼¥Ƈ⁹Ḣ


              A (very very inefficient) dyadic Link accepting s on the left and x on the right which yields the shortest possible answer as a list of integers (sorted ascending).



              Try it online! - not much point trying it for much higher values!



              How?



              x’2;’ÄÄx⁸ŒPS⁼¥Ƈ⁹Ḣ - Link: s, x e.g. 5, 17
              x - repeat (s) (x) times [5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5]
              ’ - decrement (vectorises) [4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]
              2; - prepend a two [2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]
              ’ - decrement (vectorises) [1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
              Ä - cumulative sums [1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34, 37, 40, 43, 46, 49, 52]
              Ä - cumulative sums [1, 5, 12, 22, 35, 51, 70, 92, 117, 145, 176, 210, 247, 287, 330, 376, 425, 477]
              x⁸ - repeat (each of those) (s) times [1, 1, 1, 5, ..., 425, 477, 477, 477]
              ŒP - power-set [[], [1], [1], ..., [1, 1], ..., [5, 22, 70], ... etc]
              (this has 2^(x(s+1)) entries ...this example would have 2^(17(5+1)) = 2^102 = 5070602400912917605986812821504 entries!)
              (Note: the lengths increase left to right)
              Ƈ - filter keep if:
              ¥ - last two links as a dyad:
              S - sum
              ⁼ ⁹ - equals (x)? [[5,12], ... , [5,12], [1, 1, 5, 5, 5], ... , [1, 1, 5, 5, 5], [1, 1, 1, 1, 1, 12], ...]
              Ḣ - head [5,12]





              share|improve this answer












              $endgroup$




              Jelly, 17 bytes



              x’2;’ÄÄx⁸ŒPS⁼¥Ƈ⁹Ḣ


              A (very very inefficient) dyadic Link accepting s on the left and x on the right which yields the shortest possible answer as a list of integers (sorted ascending).



              Try it online! - not much point trying it for much higher values!



              How?



              x’2;’ÄÄx⁸ŒPS⁼¥Ƈ⁹Ḣ - Link: s, x e.g. 5, 17
              x - repeat (s) (x) times [5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5]
              ’ - decrement (vectorises) [4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]
              2; - prepend a two [2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]
              ’ - decrement (vectorises) [1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
              Ä - cumulative sums [1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34, 37, 40, 43, 46, 49, 52]
              Ä - cumulative sums [1, 5, 12, 22, 35, 51, 70, 92, 117, 145, 176, 210, 247, 287, 330, 376, 425, 477]
              x⁸ - repeat (each of those) (s) times [1, 1, 1, 5, ..., 425, 477, 477, 477]
              ŒP - power-set [[], [1], [1], ..., [1, 1], ..., [5, 22, 70], ... etc]
              (this has 2^(x(s+1)) entries ...this example would have 2^(17(5+1)) = 2^102 = 5070602400912917605986812821504 entries!)
              (Note: the lengths increase left to right)
              Ƈ - filter keep if:
              ¥ - last two links as a dyad:
              S - sum
              ⁼ ⁹ - equals (x)? [[5,12], ... , [5,12], [1, 1, 5, 5, 5], ... , [1, 1, 5, 5, 5], [1, 1, 1, 1, 1, 12], ...]
              Ḣ - head [5,12]






              share|improve this answer















              share|improve this answer




              share|improve this answer



              share|improve this answer








              edited 6 hours ago

























              answered 7 hours ago









              Jonathan AllanJonathan Allan

              61.2k5 gold badges44 silver badges188 bronze badges




              61.2k5 gold badges44 silver badges188 bronze badges
























                  1
















                  $begingroup$


                  Ruby, 79 bytes



                  Computes the first $n$ $s$-gonal numbers and zero, gets the cartesian product of itself $s$ times, and finds the first entry that matches. Later test cases run out of memory on TIO but theoretically work.



                  $fracn^2(s-2)-n(s-4)2$ is reorganized into $fracn(ns-2n-s+4)2$ because it saves bytes in Ruby.





                  ->n,sa=(0..n).mapi*(i*s-2*i-s+4)/2;a.product(*[a]*~-s).finda


                  Try it online!






                  share|improve this answer










                  $endgroup$



















                    1
















                    $begingroup$


                    Ruby, 79 bytes



                    Computes the first $n$ $s$-gonal numbers and zero, gets the cartesian product of itself $s$ times, and finds the first entry that matches. Later test cases run out of memory on TIO but theoretically work.



                    $fracn^2(s-2)-n(s-4)2$ is reorganized into $fracn(ns-2n-s+4)2$ because it saves bytes in Ruby.





                    ->n,sa=(0..n).mapi*(i*s-2*i-s+4)/2;a.product(*[a]*~-s).finda


                    Try it online!






                    share|improve this answer










                    $endgroup$

















                      1














                      1










                      1







                      $begingroup$


                      Ruby, 79 bytes



                      Computes the first $n$ $s$-gonal numbers and zero, gets the cartesian product of itself $s$ times, and finds the first entry that matches. Later test cases run out of memory on TIO but theoretically work.



                      $fracn^2(s-2)-n(s-4)2$ is reorganized into $fracn(ns-2n-s+4)2$ because it saves bytes in Ruby.





                      ->n,sa=(0..n).mapi*(i*s-2*i-s+4)/2;a.product(*[a]*~-s).finda


                      Try it online!






                      share|improve this answer










                      $endgroup$




                      Ruby, 79 bytes



                      Computes the first $n$ $s$-gonal numbers and zero, gets the cartesian product of itself $s$ times, and finds the first entry that matches. Later test cases run out of memory on TIO but theoretically work.



                      $fracn^2(s-2)-n(s-4)2$ is reorganized into $fracn(ns-2n-s+4)2$ because it saves bytes in Ruby.





                      ->n,sa=(0..n).mapi*(i*s-2*i-s+4)/2;a.product(*[a]*~-s).finda


                      Try it online!







                      share|improve this answer













                      share|improve this answer




                      share|improve this answer



                      share|improve this answer










                      answered 6 hours ago









                      Value InkValue Ink

                      9,2988 silver badges37 bronze badges




                      9,2988 silver badges37 bronze badges
























                          1
















                          $begingroup$

                          JavaScript (ES6),  83  80 bytes



                          Takes input as (s)(x).





                          s=>g=(x,n=0,a=[],y=~n*(~-n-n*s/2))=>x<y?x|a[s]?0:a:g(x,n+1,a)||g(x-y,n,[...a,y])


                          Try it online!



                          Formula



                          It turns out to be shorter to start with $n=0$ and to compute $P(n+1,s)$:



                          $$beginalignP(n+1,s)&=((n+1)^2(s-2)-(n+1)(s-4))/2\
                          &=(n^2(s-2)+ns+2)/2\
                          &=-(n+1)((n-1)-ns/2)
                          endalign$$



                          which can be written in 14 bytes in JS:



                          ~n*(~-n-n*s/2)


                          Commented



                          s => // main function taking s
                          g = ( // recursive function g
                          x, // taking x
                          n = 0, // start with n = 0
                          a = [], // a[] = list of s-gonal numbers
                          y = // y = P(n + 1, s)
                          ~n * (~-n - n * s / 2) // = -(n + 1) * ((n - 1) - n * s / 2)
                          ) => //
                          x < y ? // if x is less than P(n + 1, s):
                          x | a[s] ? // if x is not equal to 0 or a[] is too long:
                          0 // failed: return 0
                          : // else:
                          a // success: return a[]
                          : // else:
                          // recursive calls:
                          g(x, n + 1, a) || // preferred: try to increment n
                          g(x - y, n, [...a, y]) // fallback : try to use the current s-gonal number





                          share|improve this answer












                          $endgroup$



















                            1
















                            $begingroup$

                            JavaScript (ES6),  83  80 bytes



                            Takes input as (s)(x).





                            s=>g=(x,n=0,a=[],y=~n*(~-n-n*s/2))=>x<y?x|a[s]?0:a:g(x,n+1,a)||g(x-y,n,[...a,y])


                            Try it online!



                            Formula



                            It turns out to be shorter to start with $n=0$ and to compute $P(n+1,s)$:



                            $$beginalignP(n+1,s)&=((n+1)^2(s-2)-(n+1)(s-4))/2\
                            &=(n^2(s-2)+ns+2)/2\
                            &=-(n+1)((n-1)-ns/2)
                            endalign$$



                            which can be written in 14 bytes in JS:



                            ~n*(~-n-n*s/2)


                            Commented



                            s => // main function taking s
                            g = ( // recursive function g
                            x, // taking x
                            n = 0, // start with n = 0
                            a = [], // a[] = list of s-gonal numbers
                            y = // y = P(n + 1, s)
                            ~n * (~-n - n * s / 2) // = -(n + 1) * ((n - 1) - n * s / 2)
                            ) => //
                            x < y ? // if x is less than P(n + 1, s):
                            x | a[s] ? // if x is not equal to 0 or a[] is too long:
                            0 // failed: return 0
                            : // else:
                            a // success: return a[]
                            : // else:
                            // recursive calls:
                            g(x, n + 1, a) || // preferred: try to increment n
                            g(x - y, n, [...a, y]) // fallback : try to use the current s-gonal number





                            share|improve this answer












                            $endgroup$

















                              1














                              1










                              1







                              $begingroup$

                              JavaScript (ES6),  83  80 bytes



                              Takes input as (s)(x).





                              s=>g=(x,n=0,a=[],y=~n*(~-n-n*s/2))=>x<y?x|a[s]?0:a:g(x,n+1,a)||g(x-y,n,[...a,y])


                              Try it online!



                              Formula



                              It turns out to be shorter to start with $n=0$ and to compute $P(n+1,s)$:



                              $$beginalignP(n+1,s)&=((n+1)^2(s-2)-(n+1)(s-4))/2\
                              &=(n^2(s-2)+ns+2)/2\
                              &=-(n+1)((n-1)-ns/2)
                              endalign$$



                              which can be written in 14 bytes in JS:



                              ~n*(~-n-n*s/2)


                              Commented



                              s => // main function taking s
                              g = ( // recursive function g
                              x, // taking x
                              n = 0, // start with n = 0
                              a = [], // a[] = list of s-gonal numbers
                              y = // y = P(n + 1, s)
                              ~n * (~-n - n * s / 2) // = -(n + 1) * ((n - 1) - n * s / 2)
                              ) => //
                              x < y ? // if x is less than P(n + 1, s):
                              x | a[s] ? // if x is not equal to 0 or a[] is too long:
                              0 // failed: return 0
                              : // else:
                              a // success: return a[]
                              : // else:
                              // recursive calls:
                              g(x, n + 1, a) || // preferred: try to increment n
                              g(x - y, n, [...a, y]) // fallback : try to use the current s-gonal number





                              share|improve this answer












                              $endgroup$



                              JavaScript (ES6),  83  80 bytes



                              Takes input as (s)(x).





                              s=>g=(x,n=0,a=[],y=~n*(~-n-n*s/2))=>x<y?x|a[s]?0:a:g(x,n+1,a)||g(x-y,n,[...a,y])


                              Try it online!



                              Formula



                              It turns out to be shorter to start with $n=0$ and to compute $P(n+1,s)$:



                              $$beginalignP(n+1,s)&=((n+1)^2(s-2)-(n+1)(s-4))/2\
                              &=(n^2(s-2)+ns+2)/2\
                              &=-(n+1)((n-1)-ns/2)
                              endalign$$



                              which can be written in 14 bytes in JS:



                              ~n*(~-n-n*s/2)


                              Commented



                              s => // main function taking s
                              g = ( // recursive function g
                              x, // taking x
                              n = 0, // start with n = 0
                              a = [], // a[] = list of s-gonal numbers
                              y = // y = P(n + 1, s)
                              ~n * (~-n - n * s / 2) // = -(n + 1) * ((n - 1) - n * s / 2)
                              ) => //
                              x < y ? // if x is less than P(n + 1, s):
                              x | a[s] ? // if x is not equal to 0 or a[] is too long:
                              0 // failed: return 0
                              : // else:
                              a // success: return a[]
                              : // else:
                              // recursive calls:
                              g(x, n + 1, a) || // preferred: try to increment n
                              g(x - y, n, [...a, y]) // fallback : try to use the current s-gonal number






                              share|improve this answer















                              share|improve this answer




                              share|improve this answer



                              share|improve this answer








                              edited 2 hours ago

























                              answered 7 hours ago









                              ArnauldArnauld

                              94.6k7 gold badges111 silver badges384 bronze badges




                              94.6k7 gold badges111 silver badges384 bronze badges
























                                  0
















                                  $begingroup$


                                  Retina, 111 bytes



                                  d+
                                  *
                                  ~(`$
                                  $"
                                  0%["^_+ "|""]'$L$`G_(?<=(?=___(_*))_+)
                                  ((_(?($.(2*$>`))$1$.(2*$>`)))$*)
                                  1%|' L$`G_
                                  $$.$.($`$>`


                                  Try it online! Link includes test cases. Takes input in the order s n. Explanation:



                                  d+
                                  *


                                  Convert to unary.



                                  ~(`


                                  After processing the remaining stages, treat them as a Retina program and execute them on the same input.



                                  $
                                  $"


                                  Duplicate the line.



                                  0%["^_+ "|""]'$L$`G_(?<=(?=___(_*))_+)
                                  ((_(?($.(2*$>`))$1$.(2*$>`)))$*)


                                  Replace the first copy with a regular expression that skips over the first number and then matches s s-gonal numbers. The numbers themselves are captured in odd capture groups and the even capture groups are used to ensure that all of the numbers are s-gonal.



                                  1%|' L$`G_
                                  $$.$.($`$>`


                                  Replace the second copy with a space-separated list of the odd capture groups.



                                  As an example, the generated code for an input of 5 17 is as follows:



                                  ^_+ ((_(?(2)__2))*)((_(?(4)__4))*)((_(?(6)__6))*)((_(?(8)__8))*)((_(?(10)__10))*)$
                                  $.1 $.3 $.5 $.7 $.9





                                  share|improve this answer










                                  $endgroup$



















                                    0
















                                    $begingroup$


                                    Retina, 111 bytes



                                    d+
                                    *
                                    ~(`$
                                    $"
                                    0%["^_+ "|""]'$L$`G_(?<=(?=___(_*))_+)
                                    ((_(?($.(2*$>`))$1$.(2*$>`)))$*)
                                    1%|' L$`G_
                                    $$.$.($`$>`


                                    Try it online! Link includes test cases. Takes input in the order s n. Explanation:



                                    d+
                                    *


                                    Convert to unary.



                                    ~(`


                                    After processing the remaining stages, treat them as a Retina program and execute them on the same input.



                                    $
                                    $"


                                    Duplicate the line.



                                    0%["^_+ "|""]'$L$`G_(?<=(?=___(_*))_+)
                                    ((_(?($.(2*$>`))$1$.(2*$>`)))$*)


                                    Replace the first copy with a regular expression that skips over the first number and then matches s s-gonal numbers. The numbers themselves are captured in odd capture groups and the even capture groups are used to ensure that all of the numbers are s-gonal.



                                    1%|' L$`G_
                                    $$.$.($`$>`


                                    Replace the second copy with a space-separated list of the odd capture groups.



                                    As an example, the generated code for an input of 5 17 is as follows:



                                    ^_+ ((_(?(2)__2))*)((_(?(4)__4))*)((_(?(6)__6))*)((_(?(8)__8))*)((_(?(10)__10))*)$
                                    $.1 $.3 $.5 $.7 $.9





                                    share|improve this answer










                                    $endgroup$

















                                      0














                                      0










                                      0







                                      $begingroup$


                                      Retina, 111 bytes



                                      d+
                                      *
                                      ~(`$
                                      $"
                                      0%["^_+ "|""]'$L$`G_(?<=(?=___(_*))_+)
                                      ((_(?($.(2*$>`))$1$.(2*$>`)))$*)
                                      1%|' L$`G_
                                      $$.$.($`$>`


                                      Try it online! Link includes test cases. Takes input in the order s n. Explanation:



                                      d+
                                      *


                                      Convert to unary.



                                      ~(`


                                      After processing the remaining stages, treat them as a Retina program and execute them on the same input.



                                      $
                                      $"


                                      Duplicate the line.



                                      0%["^_+ "|""]'$L$`G_(?<=(?=___(_*))_+)
                                      ((_(?($.(2*$>`))$1$.(2*$>`)))$*)


                                      Replace the first copy with a regular expression that skips over the first number and then matches s s-gonal numbers. The numbers themselves are captured in odd capture groups and the even capture groups are used to ensure that all of the numbers are s-gonal.



                                      1%|' L$`G_
                                      $$.$.($`$>`


                                      Replace the second copy with a space-separated list of the odd capture groups.



                                      As an example, the generated code for an input of 5 17 is as follows:



                                      ^_+ ((_(?(2)__2))*)((_(?(4)__4))*)((_(?(6)__6))*)((_(?(8)__8))*)((_(?(10)__10))*)$
                                      $.1 $.3 $.5 $.7 $.9





                                      share|improve this answer










                                      $endgroup$




                                      Retina, 111 bytes



                                      d+
                                      *
                                      ~(`$
                                      $"
                                      0%["^_+ "|""]'$L$`G_(?<=(?=___(_*))_+)
                                      ((_(?($.(2*$>`))$1$.(2*$>`)))$*)
                                      1%|' L$`G_
                                      $$.$.($`$>`


                                      Try it online! Link includes test cases. Takes input in the order s n. Explanation:



                                      d+
                                      *


                                      Convert to unary.



                                      ~(`


                                      After processing the remaining stages, treat them as a Retina program and execute them on the same input.



                                      $
                                      $"


                                      Duplicate the line.



                                      0%["^_+ "|""]'$L$`G_(?<=(?=___(_*))_+)
                                      ((_(?($.(2*$>`))$1$.(2*$>`)))$*)


                                      Replace the first copy with a regular expression that skips over the first number and then matches s s-gonal numbers. The numbers themselves are captured in odd capture groups and the even capture groups are used to ensure that all of the numbers are s-gonal.



                                      1%|' L$`G_
                                      $$.$.($`$>`


                                      Replace the second copy with a space-separated list of the odd capture groups.



                                      As an example, the generated code for an input of 5 17 is as follows:



                                      ^_+ ((_(?(2)__2))*)((_(?(4)__4))*)((_(?(6)__6))*)((_(?(8)__8))*)((_(?(10)__10))*)$
                                      $.1 $.3 $.5 $.7 $.9






                                      share|improve this answer













                                      share|improve this answer




                                      share|improve this answer



                                      share|improve this answer










                                      answered 1 hour ago









                                      NeilNeil

                                      89.7k8 gold badges46 silver badges188 bronze badges




                                      89.7k8 gold badges46 silver badges188 bronze badges































                                          draft saved

                                          draft discarded















































                                          If this is an answer to a challenge…



                                          • …Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.


                                          • …Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
                                            Explanations of your answer make it more interesting to read and are very much encouraged.


                                          • …Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.


                                          More generally…



                                          • …Please make sure to answer the question and provide sufficient detail.


                                          • …Avoid asking for help, clarification or responding to other answers (use comments instead).




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function ()
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f194042%2ffermats-polygonal-number-theorem%23new-answer', 'question_page');

                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown









                                          Popular posts from this blog

                                          Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

                                          Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

                                          Ласкавець круглолистий Зміст Опис | Поширення | Галерея | Примітки | Посилання | Навігаційне меню58171138361-22960890446Bupleurum rotundifoliumEuro+Med PlantbasePlants of the World Online — Kew ScienceGermplasm Resources Information Network (GRIN)Ласкавецькн. VI : Літери Ком — Левиправивши або дописавши її