Partitioning the grid into triangles











up vote
10
down vote

favorite












Goal



The goal of this challenge is to produce a function of n which computes the number of ways to partition the n X 1 grid into triangles where all of the vertices of the triangles are on grid points.



Example



For example, there are 14 ways to partition the 2 x 1 grid, so f(2) = 14 via the following partitions Partitions of 2 x 1
where the partitions have 2, 2, 2, 2, 4, and 2 distinct orientations respectively.



Scoring



This is code-golf, so shortest code wins.










share|improve this question




















  • 8




    Some additional test cases would be beneficial, so we can verify our submissions are correct.
    – AdmBorkBork
    6 hours ago






  • 6




    You may want to specify non-degenerate triangles.
    – Arnauld
    6 hours ago















up vote
10
down vote

favorite












Goal



The goal of this challenge is to produce a function of n which computes the number of ways to partition the n X 1 grid into triangles where all of the vertices of the triangles are on grid points.



Example



For example, there are 14 ways to partition the 2 x 1 grid, so f(2) = 14 via the following partitions Partitions of 2 x 1
where the partitions have 2, 2, 2, 2, 4, and 2 distinct orientations respectively.



Scoring



This is code-golf, so shortest code wins.










share|improve this question




















  • 8




    Some additional test cases would be beneficial, so we can verify our submissions are correct.
    – AdmBorkBork
    6 hours ago






  • 6




    You may want to specify non-degenerate triangles.
    – Arnauld
    6 hours ago













up vote
10
down vote

favorite









up vote
10
down vote

favorite











Goal



The goal of this challenge is to produce a function of n which computes the number of ways to partition the n X 1 grid into triangles where all of the vertices of the triangles are on grid points.



Example



For example, there are 14 ways to partition the 2 x 1 grid, so f(2) = 14 via the following partitions Partitions of 2 x 1
where the partitions have 2, 2, 2, 2, 4, and 2 distinct orientations respectively.



Scoring



This is code-golf, so shortest code wins.










share|improve this question















Goal



The goal of this challenge is to produce a function of n which computes the number of ways to partition the n X 1 grid into triangles where all of the vertices of the triangles are on grid points.



Example



For example, there are 14 ways to partition the 2 x 1 grid, so f(2) = 14 via the following partitions Partitions of 2 x 1
where the partitions have 2, 2, 2, 2, 4, and 2 distinct orientations respectively.



Scoring



This is code-golf, so shortest code wins.







code-golf geometry combinatorics grid






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 6 hours ago

























asked 6 hours ago









Peter Kagey

602515




602515








  • 8




    Some additional test cases would be beneficial, so we can verify our submissions are correct.
    – AdmBorkBork
    6 hours ago






  • 6




    You may want to specify non-degenerate triangles.
    – Arnauld
    6 hours ago














  • 8




    Some additional test cases would be beneficial, so we can verify our submissions are correct.
    – AdmBorkBork
    6 hours ago






  • 6




    You may want to specify non-degenerate triangles.
    – Arnauld
    6 hours ago








8




8




Some additional test cases would be beneficial, so we can verify our submissions are correct.
– AdmBorkBork
6 hours ago




Some additional test cases would be beneficial, so we can verify our submissions are correct.
– AdmBorkBork
6 hours ago




6




6




You may want to specify non-degenerate triangles.
– Arnauld
6 hours ago




You may want to specify non-degenerate triangles.
– Arnauld
6 hours ago










7 Answers
7






active

oldest

votes

















up vote
8
down vote














Haskell, 60 55 54 bytes



After a drawing and programming a lot of examples, it occured to me that this is the same as the problem of the rooks:




On a $(n+1) times (n+1)$ chessboard, how many ways are there for a rook to go from $(0,0)$ to $(n,n)$ by just moving right $+(1,0)$ or up $+(0,1)$?




Basically you have the top and the bottom line. Now you have to fill in the non-vertical line. Each triangle must have two non-vertical line. Whether one of its sides is part of the top or the bottom line corresponds to the direction and length you'd go in the rooks problem. This is OEIS A051708. As an illustration of this correspondence consider following examples. Here the top line corresponds to up-moves, while the bottom line corresponds to right-moves.





Thanks @PeterTaylor for -6 bytes!





b 0=1
b 1=2
b n=((10*n-6)*b(n-1)-9*(n-2)*b(n-2))`div`n


Try it online!






share|improve this answer























  • I found the OEIS sequence by searching with the first few values. Nice explanation for why it matches. Do you want to edit it to add a comment about this alternative combinatorial interpretation? If not, I might.
    – Peter Taylor
    5 hours ago










  • BTW you need to adjust the indexing, because the correct answer here is A051708(n+1). So I posted the first correct answer :-P
    – Peter Taylor
    5 hours ago












  • I take it the rook moves map to triangles by making triangles with top and bottom edges correspond to up or right moves?
    – Neil
    5 hours ago










  • @PeterTaylor Damn, thanks for pointing out my mistake :)
    – flawr
    4 hours ago






  • 4




    @Neil I added a graphical explanation.
    – flawr
    4 hours ago


















up vote
5
down vote













CJam (28 bytes)



{_1aa{_2$,f{j}@@,f{j}+1b}2j}


Online demo



Dissection



The triangles all have one horizontal edge and two edges which link the horizontal lines. Label the non-horizontal edges by a tuple of their two x-coords and sort lexicographically. Then the first edge is (0,0), the last edge is (n,n), and two consecutive edges differ in precisely one of the two positions. This makes for a simple recursion, which I've implemented using the memoised recursion operator j:



{            e# Define a block
_ e# Duplicate the argument to get n n
1aa e# Base case for recursion: 0 0 => 1
{ e# Recursive body taking args a b
_2$,f{j} e# Recurse on 0 b up to a-1 b
@@,f{j} e# Recurse on a 0 up to a b-1
+1b e# Combine and sum
}2j e# Memoised recursion with 2 args
}


Note



This is not the first time I've wanted fj to be supported in CJam. Perhaps I should try to write a patch...






share|improve this answer























  • Yay, I beat you by 10 seconds, I don't think I was ever that close :)
    – flawr
    5 hours ago










  • @flawr, I did consider posting before writing a dissection, but I thought I had time to knock it out quickly. Then I saw "New answer", so I deleted my part-written dissection, posted, and edited.
    – Peter Taylor
    5 hours ago






  • 1




    Thanks for -5 bytes btw :D
    – flawr
    4 hours ago


















up vote
1
down vote














Python 3, 51 bytes





lambda n:-~n*(n<2)or(10-6/n)*f(n-1)-(9-18/n)*f(n-2)


Try it online!



Port of flawr's answer






share|improve this answer






























    up vote
    1
    down vote













    JavaScript (ES6),  45  44 bytes



    Uses the recursive formula found by Peter Taylor and flawr.





    f=n=>n<2?n+1:((10-6/n)*f(--n)+9/~n*f(--n)*n)


    Try it online!






    share|improve this answer



















    • 1




      See my comments on flawr's post. This has the same off-by-one error that their original version had.
      – Peter Taylor
      4 hours ago










    • @PeterTaylor Oh, I see. Thanks! Fixed now.
      – Arnauld
      4 hours ago


















    up vote
    1
    down vote














    Haskell, 54 bytes





    0%0=1
    a%b=sum$map(a%)[0..b-1]++map(%b)[0..a-1]
    f n=n%n


    Try it online!



    No formula, just a slow direct calculation.






    share|improve this answer




























      up vote
      1
      down vote














      Charcoal, 44 31 bytes



      crossed out 44 is still regular 44



      F⊕θ«≔⟦⟧ηF⊕θ⊞ηΣ∨⁺ηEυ§λκ¹⊞υη»I⊟⊟υ


      Try it online! Explanation: Works by calculating the number of ways to partition a trapezium of opposite side lengths m,n into triangles which all lie on integer offsets. This is simply a general case of the rectangle of size n in the question. The number of partitions is given recursively as the sums of the numbers of partitions for all sides m,0..n-1 and n,0..m-1. This is equivalent to generalised problem of the rooks, OEIS A035002. The code simply calculates the number of partitions working from 0,0 up to n,n using the previously calculated values as it goes.



      F⊕θ«


      Loop over the rows 0..n.



      ≔⟦⟧η


      Start with an empty row.



      F⊕θ


      Loop over the columns in the row 0..n.



      ⊞ηΣ∨⁺ηEυ§λκ¹


      Take the row so far and the values in the previous rows at the current column, and add the sum total to the current row. However, if there are no values at all, then substitute 1 in place of the sum.



      ⊞υη»


      Add the finished row to the list of rows so far.



      I⊟⊟υ


      Output the final value calculated.






      share|improve this answer






























        up vote
        1
        down vote














        Jelly, 15 bytes



        Ø.xŒ!QŒɠ€2*HP€S


        Try it online!



        Uses flawr's illustration directly, instead of the resulting formula.



        How it works



        Ø.xŒ!QŒɠ€2*HP€S    Main link (monad). Input: positive integer N.
        Ø.x Make an array containing N zeros and ones
        Œ!Q All unique permutations
        Œɠ€ Run-length encode on each permutation
        2*H Raise each value to power of 2, and halve
        P€S Product of each and sum


        Take every possible route on a square grid. The number of ways to move L units in one direction as a rook is 2**(L-1). Apply this to every route and sum the number of ways to traverse each route.






        share|improve this answer





















          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: 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/3.0/"u003ecc by-sa 3.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%2f176646%2fpartitioning-the-grid-into-triangles%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          7 Answers
          7






          active

          oldest

          votes








          7 Answers
          7






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          8
          down vote














          Haskell, 60 55 54 bytes



          After a drawing and programming a lot of examples, it occured to me that this is the same as the problem of the rooks:




          On a $(n+1) times (n+1)$ chessboard, how many ways are there for a rook to go from $(0,0)$ to $(n,n)$ by just moving right $+(1,0)$ or up $+(0,1)$?




          Basically you have the top and the bottom line. Now you have to fill in the non-vertical line. Each triangle must have two non-vertical line. Whether one of its sides is part of the top or the bottom line corresponds to the direction and length you'd go in the rooks problem. This is OEIS A051708. As an illustration of this correspondence consider following examples. Here the top line corresponds to up-moves, while the bottom line corresponds to right-moves.





          Thanks @PeterTaylor for -6 bytes!





          b 0=1
          b 1=2
          b n=((10*n-6)*b(n-1)-9*(n-2)*b(n-2))`div`n


          Try it online!






          share|improve this answer























          • I found the OEIS sequence by searching with the first few values. Nice explanation for why it matches. Do you want to edit it to add a comment about this alternative combinatorial interpretation? If not, I might.
            – Peter Taylor
            5 hours ago










          • BTW you need to adjust the indexing, because the correct answer here is A051708(n+1). So I posted the first correct answer :-P
            – Peter Taylor
            5 hours ago












          • I take it the rook moves map to triangles by making triangles with top and bottom edges correspond to up or right moves?
            – Neil
            5 hours ago










          • @PeterTaylor Damn, thanks for pointing out my mistake :)
            – flawr
            4 hours ago






          • 4




            @Neil I added a graphical explanation.
            – flawr
            4 hours ago















          up vote
          8
          down vote














          Haskell, 60 55 54 bytes



          After a drawing and programming a lot of examples, it occured to me that this is the same as the problem of the rooks:




          On a $(n+1) times (n+1)$ chessboard, how many ways are there for a rook to go from $(0,0)$ to $(n,n)$ by just moving right $+(1,0)$ or up $+(0,1)$?




          Basically you have the top and the bottom line. Now you have to fill in the non-vertical line. Each triangle must have two non-vertical line. Whether one of its sides is part of the top or the bottom line corresponds to the direction and length you'd go in the rooks problem. This is OEIS A051708. As an illustration of this correspondence consider following examples. Here the top line corresponds to up-moves, while the bottom line corresponds to right-moves.





          Thanks @PeterTaylor for -6 bytes!





          b 0=1
          b 1=2
          b n=((10*n-6)*b(n-1)-9*(n-2)*b(n-2))`div`n


          Try it online!






          share|improve this answer























          • I found the OEIS sequence by searching with the first few values. Nice explanation for why it matches. Do you want to edit it to add a comment about this alternative combinatorial interpretation? If not, I might.
            – Peter Taylor
            5 hours ago










          • BTW you need to adjust the indexing, because the correct answer here is A051708(n+1). So I posted the first correct answer :-P
            – Peter Taylor
            5 hours ago












          • I take it the rook moves map to triangles by making triangles with top and bottom edges correspond to up or right moves?
            – Neil
            5 hours ago










          • @PeterTaylor Damn, thanks for pointing out my mistake :)
            – flawr
            4 hours ago






          • 4




            @Neil I added a graphical explanation.
            – flawr
            4 hours ago













          up vote
          8
          down vote










          up vote
          8
          down vote










          Haskell, 60 55 54 bytes



          After a drawing and programming a lot of examples, it occured to me that this is the same as the problem of the rooks:




          On a $(n+1) times (n+1)$ chessboard, how many ways are there for a rook to go from $(0,0)$ to $(n,n)$ by just moving right $+(1,0)$ or up $+(0,1)$?




          Basically you have the top and the bottom line. Now you have to fill in the non-vertical line. Each triangle must have two non-vertical line. Whether one of its sides is part of the top or the bottom line corresponds to the direction and length you'd go in the rooks problem. This is OEIS A051708. As an illustration of this correspondence consider following examples. Here the top line corresponds to up-moves, while the bottom line corresponds to right-moves.





          Thanks @PeterTaylor for -6 bytes!





          b 0=1
          b 1=2
          b n=((10*n-6)*b(n-1)-9*(n-2)*b(n-2))`div`n


          Try it online!






          share|improve this answer















          Haskell, 60 55 54 bytes



          After a drawing and programming a lot of examples, it occured to me that this is the same as the problem of the rooks:




          On a $(n+1) times (n+1)$ chessboard, how many ways are there for a rook to go from $(0,0)$ to $(n,n)$ by just moving right $+(1,0)$ or up $+(0,1)$?




          Basically you have the top and the bottom line. Now you have to fill in the non-vertical line. Each triangle must have two non-vertical line. Whether one of its sides is part of the top or the bottom line corresponds to the direction and length you'd go in the rooks problem. This is OEIS A051708. As an illustration of this correspondence consider following examples. Here the top line corresponds to up-moves, while the bottom line corresponds to right-moves.





          Thanks @PeterTaylor for -6 bytes!





          b 0=1
          b 1=2
          b n=((10*n-6)*b(n-1)-9*(n-2)*b(n-2))`div`n


          Try it online!







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 4 hours ago

























          answered 5 hours ago









          flawr

          26.2k662183




          26.2k662183












          • I found the OEIS sequence by searching with the first few values. Nice explanation for why it matches. Do you want to edit it to add a comment about this alternative combinatorial interpretation? If not, I might.
            – Peter Taylor
            5 hours ago










          • BTW you need to adjust the indexing, because the correct answer here is A051708(n+1). So I posted the first correct answer :-P
            – Peter Taylor
            5 hours ago












          • I take it the rook moves map to triangles by making triangles with top and bottom edges correspond to up or right moves?
            – Neil
            5 hours ago










          • @PeterTaylor Damn, thanks for pointing out my mistake :)
            – flawr
            4 hours ago






          • 4




            @Neil I added a graphical explanation.
            – flawr
            4 hours ago


















          • I found the OEIS sequence by searching with the first few values. Nice explanation for why it matches. Do you want to edit it to add a comment about this alternative combinatorial interpretation? If not, I might.
            – Peter Taylor
            5 hours ago










          • BTW you need to adjust the indexing, because the correct answer here is A051708(n+1). So I posted the first correct answer :-P
            – Peter Taylor
            5 hours ago












          • I take it the rook moves map to triangles by making triangles with top and bottom edges correspond to up or right moves?
            – Neil
            5 hours ago










          • @PeterTaylor Damn, thanks for pointing out my mistake :)
            – flawr
            4 hours ago






          • 4




            @Neil I added a graphical explanation.
            – flawr
            4 hours ago
















          I found the OEIS sequence by searching with the first few values. Nice explanation for why it matches. Do you want to edit it to add a comment about this alternative combinatorial interpretation? If not, I might.
          – Peter Taylor
          5 hours ago




          I found the OEIS sequence by searching with the first few values. Nice explanation for why it matches. Do you want to edit it to add a comment about this alternative combinatorial interpretation? If not, I might.
          – Peter Taylor
          5 hours ago












          BTW you need to adjust the indexing, because the correct answer here is A051708(n+1). So I posted the first correct answer :-P
          – Peter Taylor
          5 hours ago






          BTW you need to adjust the indexing, because the correct answer here is A051708(n+1). So I posted the first correct answer :-P
          – Peter Taylor
          5 hours ago














          I take it the rook moves map to triangles by making triangles with top and bottom edges correspond to up or right moves?
          – Neil
          5 hours ago




          I take it the rook moves map to triangles by making triangles with top and bottom edges correspond to up or right moves?
          – Neil
          5 hours ago












          @PeterTaylor Damn, thanks for pointing out my mistake :)
          – flawr
          4 hours ago




          @PeterTaylor Damn, thanks for pointing out my mistake :)
          – flawr
          4 hours ago




          4




          4




          @Neil I added a graphical explanation.
          – flawr
          4 hours ago




          @Neil I added a graphical explanation.
          – flawr
          4 hours ago










          up vote
          5
          down vote













          CJam (28 bytes)



          {_1aa{_2$,f{j}@@,f{j}+1b}2j}


          Online demo



          Dissection



          The triangles all have one horizontal edge and two edges which link the horizontal lines. Label the non-horizontal edges by a tuple of their two x-coords and sort lexicographically. Then the first edge is (0,0), the last edge is (n,n), and two consecutive edges differ in precisely one of the two positions. This makes for a simple recursion, which I've implemented using the memoised recursion operator j:



          {            e# Define a block
          _ e# Duplicate the argument to get n n
          1aa e# Base case for recursion: 0 0 => 1
          { e# Recursive body taking args a b
          _2$,f{j} e# Recurse on 0 b up to a-1 b
          @@,f{j} e# Recurse on a 0 up to a b-1
          +1b e# Combine and sum
          }2j e# Memoised recursion with 2 args
          }


          Note



          This is not the first time I've wanted fj to be supported in CJam. Perhaps I should try to write a patch...






          share|improve this answer























          • Yay, I beat you by 10 seconds, I don't think I was ever that close :)
            – flawr
            5 hours ago










          • @flawr, I did consider posting before writing a dissection, but I thought I had time to knock it out quickly. Then I saw "New answer", so I deleted my part-written dissection, posted, and edited.
            – Peter Taylor
            5 hours ago






          • 1




            Thanks for -5 bytes btw :D
            – flawr
            4 hours ago















          up vote
          5
          down vote













          CJam (28 bytes)



          {_1aa{_2$,f{j}@@,f{j}+1b}2j}


          Online demo



          Dissection



          The triangles all have one horizontal edge and two edges which link the horizontal lines. Label the non-horizontal edges by a tuple of their two x-coords and sort lexicographically. Then the first edge is (0,0), the last edge is (n,n), and two consecutive edges differ in precisely one of the two positions. This makes for a simple recursion, which I've implemented using the memoised recursion operator j:



          {            e# Define a block
          _ e# Duplicate the argument to get n n
          1aa e# Base case for recursion: 0 0 => 1
          { e# Recursive body taking args a b
          _2$,f{j} e# Recurse on 0 b up to a-1 b
          @@,f{j} e# Recurse on a 0 up to a b-1
          +1b e# Combine and sum
          }2j e# Memoised recursion with 2 args
          }


          Note



          This is not the first time I've wanted fj to be supported in CJam. Perhaps I should try to write a patch...






          share|improve this answer























          • Yay, I beat you by 10 seconds, I don't think I was ever that close :)
            – flawr
            5 hours ago










          • @flawr, I did consider posting before writing a dissection, but I thought I had time to knock it out quickly. Then I saw "New answer", so I deleted my part-written dissection, posted, and edited.
            – Peter Taylor
            5 hours ago






          • 1




            Thanks for -5 bytes btw :D
            – flawr
            4 hours ago













          up vote
          5
          down vote










          up vote
          5
          down vote









          CJam (28 bytes)



          {_1aa{_2$,f{j}@@,f{j}+1b}2j}


          Online demo



          Dissection



          The triangles all have one horizontal edge and two edges which link the horizontal lines. Label the non-horizontal edges by a tuple of their two x-coords and sort lexicographically. Then the first edge is (0,0), the last edge is (n,n), and two consecutive edges differ in precisely one of the two positions. This makes for a simple recursion, which I've implemented using the memoised recursion operator j:



          {            e# Define a block
          _ e# Duplicate the argument to get n n
          1aa e# Base case for recursion: 0 0 => 1
          { e# Recursive body taking args a b
          _2$,f{j} e# Recurse on 0 b up to a-1 b
          @@,f{j} e# Recurse on a 0 up to a b-1
          +1b e# Combine and sum
          }2j e# Memoised recursion with 2 args
          }


          Note



          This is not the first time I've wanted fj to be supported in CJam. Perhaps I should try to write a patch...






          share|improve this answer














          CJam (28 bytes)



          {_1aa{_2$,f{j}@@,f{j}+1b}2j}


          Online demo



          Dissection



          The triangles all have one horizontal edge and two edges which link the horizontal lines. Label the non-horizontal edges by a tuple of their two x-coords and sort lexicographically. Then the first edge is (0,0), the last edge is (n,n), and two consecutive edges differ in precisely one of the two positions. This makes for a simple recursion, which I've implemented using the memoised recursion operator j:



          {            e# Define a block
          _ e# Duplicate the argument to get n n
          1aa e# Base case for recursion: 0 0 => 1
          { e# Recursive body taking args a b
          _2$,f{j} e# Recurse on 0 b up to a-1 b
          @@,f{j} e# Recurse on a 0 up to a b-1
          +1b e# Combine and sum
          }2j e# Memoised recursion with 2 args
          }


          Note



          This is not the first time I've wanted fj to be supported in CJam. Perhaps I should try to write a patch...







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 4 hours ago

























          answered 5 hours ago









          Peter Taylor

          38.9k453142




          38.9k453142












          • Yay, I beat you by 10 seconds, I don't think I was ever that close :)
            – flawr
            5 hours ago










          • @flawr, I did consider posting before writing a dissection, but I thought I had time to knock it out quickly. Then I saw "New answer", so I deleted my part-written dissection, posted, and edited.
            – Peter Taylor
            5 hours ago






          • 1




            Thanks for -5 bytes btw :D
            – flawr
            4 hours ago


















          • Yay, I beat you by 10 seconds, I don't think I was ever that close :)
            – flawr
            5 hours ago










          • @flawr, I did consider posting before writing a dissection, but I thought I had time to knock it out quickly. Then I saw "New answer", so I deleted my part-written dissection, posted, and edited.
            – Peter Taylor
            5 hours ago






          • 1




            Thanks for -5 bytes btw :D
            – flawr
            4 hours ago
















          Yay, I beat you by 10 seconds, I don't think I was ever that close :)
          – flawr
          5 hours ago




          Yay, I beat you by 10 seconds, I don't think I was ever that close :)
          – flawr
          5 hours ago












          @flawr, I did consider posting before writing a dissection, but I thought I had time to knock it out quickly. Then I saw "New answer", so I deleted my part-written dissection, posted, and edited.
          – Peter Taylor
          5 hours ago




          @flawr, I did consider posting before writing a dissection, but I thought I had time to knock it out quickly. Then I saw "New answer", so I deleted my part-written dissection, posted, and edited.
          – Peter Taylor
          5 hours ago




          1




          1




          Thanks for -5 bytes btw :D
          – flawr
          4 hours ago




          Thanks for -5 bytes btw :D
          – flawr
          4 hours ago










          up vote
          1
          down vote














          Python 3, 51 bytes





          lambda n:-~n*(n<2)or(10-6/n)*f(n-1)-(9-18/n)*f(n-2)


          Try it online!



          Port of flawr's answer






          share|improve this answer



























            up vote
            1
            down vote














            Python 3, 51 bytes





            lambda n:-~n*(n<2)or(10-6/n)*f(n-1)-(9-18/n)*f(n-2)


            Try it online!



            Port of flawr's answer






            share|improve this answer

























              up vote
              1
              down vote










              up vote
              1
              down vote










              Python 3, 51 bytes





              lambda n:-~n*(n<2)or(10-6/n)*f(n-1)-(9-18/n)*f(n-2)


              Try it online!



              Port of flawr's answer






              share|improve this answer















              Python 3, 51 bytes





              lambda n:-~n*(n<2)or(10-6/n)*f(n-1)-(9-18/n)*f(n-2)


              Try it online!



              Port of flawr's answer







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited 4 hours ago

























              answered 4 hours ago









              lirtosiast

              15.5k436105




              15.5k436105






















                  up vote
                  1
                  down vote













                  JavaScript (ES6),  45  44 bytes



                  Uses the recursive formula found by Peter Taylor and flawr.





                  f=n=>n<2?n+1:((10-6/n)*f(--n)+9/~n*f(--n)*n)


                  Try it online!






                  share|improve this answer



















                  • 1




                    See my comments on flawr's post. This has the same off-by-one error that their original version had.
                    – Peter Taylor
                    4 hours ago










                  • @PeterTaylor Oh, I see. Thanks! Fixed now.
                    – Arnauld
                    4 hours ago















                  up vote
                  1
                  down vote













                  JavaScript (ES6),  45  44 bytes



                  Uses the recursive formula found by Peter Taylor and flawr.





                  f=n=>n<2?n+1:((10-6/n)*f(--n)+9/~n*f(--n)*n)


                  Try it online!






                  share|improve this answer



















                  • 1




                    See my comments on flawr's post. This has the same off-by-one error that their original version had.
                    – Peter Taylor
                    4 hours ago










                  • @PeterTaylor Oh, I see. Thanks! Fixed now.
                    – Arnauld
                    4 hours ago













                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  JavaScript (ES6),  45  44 bytes



                  Uses the recursive formula found by Peter Taylor and flawr.





                  f=n=>n<2?n+1:((10-6/n)*f(--n)+9/~n*f(--n)*n)


                  Try it online!






                  share|improve this answer














                  JavaScript (ES6),  45  44 bytes



                  Uses the recursive formula found by Peter Taylor and flawr.





                  f=n=>n<2?n+1:((10-6/n)*f(--n)+9/~n*f(--n)*n)


                  Try it online!







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 4 hours ago

























                  answered 4 hours ago









                  Arnauld

                  69.9k686295




                  69.9k686295








                  • 1




                    See my comments on flawr's post. This has the same off-by-one error that their original version had.
                    – Peter Taylor
                    4 hours ago










                  • @PeterTaylor Oh, I see. Thanks! Fixed now.
                    – Arnauld
                    4 hours ago














                  • 1




                    See my comments on flawr's post. This has the same off-by-one error that their original version had.
                    – Peter Taylor
                    4 hours ago










                  • @PeterTaylor Oh, I see. Thanks! Fixed now.
                    – Arnauld
                    4 hours ago








                  1




                  1




                  See my comments on flawr's post. This has the same off-by-one error that their original version had.
                  – Peter Taylor
                  4 hours ago




                  See my comments on flawr's post. This has the same off-by-one error that their original version had.
                  – Peter Taylor
                  4 hours ago












                  @PeterTaylor Oh, I see. Thanks! Fixed now.
                  – Arnauld
                  4 hours ago




                  @PeterTaylor Oh, I see. Thanks! Fixed now.
                  – Arnauld
                  4 hours ago










                  up vote
                  1
                  down vote














                  Haskell, 54 bytes





                  0%0=1
                  a%b=sum$map(a%)[0..b-1]++map(%b)[0..a-1]
                  f n=n%n


                  Try it online!



                  No formula, just a slow direct calculation.






                  share|improve this answer

























                    up vote
                    1
                    down vote














                    Haskell, 54 bytes





                    0%0=1
                    a%b=sum$map(a%)[0..b-1]++map(%b)[0..a-1]
                    f n=n%n


                    Try it online!



                    No formula, just a slow direct calculation.






                    share|improve this answer























                      up vote
                      1
                      down vote










                      up vote
                      1
                      down vote










                      Haskell, 54 bytes





                      0%0=1
                      a%b=sum$map(a%)[0..b-1]++map(%b)[0..a-1]
                      f n=n%n


                      Try it online!



                      No formula, just a slow direct calculation.






                      share|improve this answer













                      Haskell, 54 bytes





                      0%0=1
                      a%b=sum$map(a%)[0..b-1]++map(%b)[0..a-1]
                      f n=n%n


                      Try it online!



                      No formula, just a slow direct calculation.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered 3 hours ago









                      xnor

                      89k18184437




                      89k18184437






















                          up vote
                          1
                          down vote














                          Charcoal, 44 31 bytes



                          crossed out 44 is still regular 44



                          F⊕θ«≔⟦⟧ηF⊕θ⊞ηΣ∨⁺ηEυ§λκ¹⊞υη»I⊟⊟υ


                          Try it online! Explanation: Works by calculating the number of ways to partition a trapezium of opposite side lengths m,n into triangles which all lie on integer offsets. This is simply a general case of the rectangle of size n in the question. The number of partitions is given recursively as the sums of the numbers of partitions for all sides m,0..n-1 and n,0..m-1. This is equivalent to generalised problem of the rooks, OEIS A035002. The code simply calculates the number of partitions working from 0,0 up to n,n using the previously calculated values as it goes.



                          F⊕θ«


                          Loop over the rows 0..n.



                          ≔⟦⟧η


                          Start with an empty row.



                          F⊕θ


                          Loop over the columns in the row 0..n.



                          ⊞ηΣ∨⁺ηEυ§λκ¹


                          Take the row so far and the values in the previous rows at the current column, and add the sum total to the current row. However, if there are no values at all, then substitute 1 in place of the sum.



                          ⊞υη»


                          Add the finished row to the list of rows so far.



                          I⊟⊟υ


                          Output the final value calculated.






                          share|improve this answer



























                            up vote
                            1
                            down vote














                            Charcoal, 44 31 bytes



                            crossed out 44 is still regular 44



                            F⊕θ«≔⟦⟧ηF⊕θ⊞ηΣ∨⁺ηEυ§λκ¹⊞υη»I⊟⊟υ


                            Try it online! Explanation: Works by calculating the number of ways to partition a trapezium of opposite side lengths m,n into triangles which all lie on integer offsets. This is simply a general case of the rectangle of size n in the question. The number of partitions is given recursively as the sums of the numbers of partitions for all sides m,0..n-1 and n,0..m-1. This is equivalent to generalised problem of the rooks, OEIS A035002. The code simply calculates the number of partitions working from 0,0 up to n,n using the previously calculated values as it goes.



                            F⊕θ«


                            Loop over the rows 0..n.



                            ≔⟦⟧η


                            Start with an empty row.



                            F⊕θ


                            Loop over the columns in the row 0..n.



                            ⊞ηΣ∨⁺ηEυ§λκ¹


                            Take the row so far and the values in the previous rows at the current column, and add the sum total to the current row. However, if there are no values at all, then substitute 1 in place of the sum.



                            ⊞υη»


                            Add the finished row to the list of rows so far.



                            I⊟⊟υ


                            Output the final value calculated.






                            share|improve this answer

























                              up vote
                              1
                              down vote










                              up vote
                              1
                              down vote










                              Charcoal, 44 31 bytes



                              crossed out 44 is still regular 44



                              F⊕θ«≔⟦⟧ηF⊕θ⊞ηΣ∨⁺ηEυ§λκ¹⊞υη»I⊟⊟υ


                              Try it online! Explanation: Works by calculating the number of ways to partition a trapezium of opposite side lengths m,n into triangles which all lie on integer offsets. This is simply a general case of the rectangle of size n in the question. The number of partitions is given recursively as the sums of the numbers of partitions for all sides m,0..n-1 and n,0..m-1. This is equivalent to generalised problem of the rooks, OEIS A035002. The code simply calculates the number of partitions working from 0,0 up to n,n using the previously calculated values as it goes.



                              F⊕θ«


                              Loop over the rows 0..n.



                              ≔⟦⟧η


                              Start with an empty row.



                              F⊕θ


                              Loop over the columns in the row 0..n.



                              ⊞ηΣ∨⁺ηEυ§λκ¹


                              Take the row so far and the values in the previous rows at the current column, and add the sum total to the current row. However, if there are no values at all, then substitute 1 in place of the sum.



                              ⊞υη»


                              Add the finished row to the list of rows so far.



                              I⊟⊟υ


                              Output the final value calculated.






                              share|improve this answer















                              Charcoal, 44 31 bytes



                              crossed out 44 is still regular 44



                              F⊕θ«≔⟦⟧ηF⊕θ⊞ηΣ∨⁺ηEυ§λκ¹⊞υη»I⊟⊟υ


                              Try it online! Explanation: Works by calculating the number of ways to partition a trapezium of opposite side lengths m,n into triangles which all lie on integer offsets. This is simply a general case of the rectangle of size n in the question. The number of partitions is given recursively as the sums of the numbers of partitions for all sides m,0..n-1 and n,0..m-1. This is equivalent to generalised problem of the rooks, OEIS A035002. The code simply calculates the number of partitions working from 0,0 up to n,n using the previously calculated values as it goes.



                              F⊕θ«


                              Loop over the rows 0..n.



                              ≔⟦⟧η


                              Start with an empty row.



                              F⊕θ


                              Loop over the columns in the row 0..n.



                              ⊞ηΣ∨⁺ηEυ§λκ¹


                              Take the row so far and the values in the previous rows at the current column, and add the sum total to the current row. However, if there are no values at all, then substitute 1 in place of the sum.



                              ⊞υη»


                              Add the finished row to the list of rows so far.



                              I⊟⊟υ


                              Output the final value calculated.







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited 3 hours ago

























                              answered 3 hours ago









                              Neil

                              78.2k744175




                              78.2k744175






















                                  up vote
                                  1
                                  down vote














                                  Jelly, 15 bytes



                                  Ø.xŒ!QŒɠ€2*HP€S


                                  Try it online!



                                  Uses flawr's illustration directly, instead of the resulting formula.



                                  How it works



                                  Ø.xŒ!QŒɠ€2*HP€S    Main link (monad). Input: positive integer N.
                                  Ø.x Make an array containing N zeros and ones
                                  Œ!Q All unique permutations
                                  Œɠ€ Run-length encode on each permutation
                                  2*H Raise each value to power of 2, and halve
                                  P€S Product of each and sum


                                  Take every possible route on a square grid. The number of ways to move L units in one direction as a rook is 2**(L-1). Apply this to every route and sum the number of ways to traverse each route.






                                  share|improve this answer

























                                    up vote
                                    1
                                    down vote














                                    Jelly, 15 bytes



                                    Ø.xŒ!QŒɠ€2*HP€S


                                    Try it online!



                                    Uses flawr's illustration directly, instead of the resulting formula.



                                    How it works



                                    Ø.xŒ!QŒɠ€2*HP€S    Main link (monad). Input: positive integer N.
                                    Ø.x Make an array containing N zeros and ones
                                    Œ!Q All unique permutations
                                    Œɠ€ Run-length encode on each permutation
                                    2*H Raise each value to power of 2, and halve
                                    P€S Product of each and sum


                                    Take every possible route on a square grid. The number of ways to move L units in one direction as a rook is 2**(L-1). Apply this to every route and sum the number of ways to traverse each route.






                                    share|improve this answer























                                      up vote
                                      1
                                      down vote










                                      up vote
                                      1
                                      down vote










                                      Jelly, 15 bytes



                                      Ø.xŒ!QŒɠ€2*HP€S


                                      Try it online!



                                      Uses flawr's illustration directly, instead of the resulting formula.



                                      How it works



                                      Ø.xŒ!QŒɠ€2*HP€S    Main link (monad). Input: positive integer N.
                                      Ø.x Make an array containing N zeros and ones
                                      Œ!Q All unique permutations
                                      Œɠ€ Run-length encode on each permutation
                                      2*H Raise each value to power of 2, and halve
                                      P€S Product of each and sum


                                      Take every possible route on a square grid. The number of ways to move L units in one direction as a rook is 2**(L-1). Apply this to every route and sum the number of ways to traverse each route.






                                      share|improve this answer













                                      Jelly, 15 bytes



                                      Ø.xŒ!QŒɠ€2*HP€S


                                      Try it online!



                                      Uses flawr's illustration directly, instead of the resulting formula.



                                      How it works



                                      Ø.xŒ!QŒɠ€2*HP€S    Main link (monad). Input: positive integer N.
                                      Ø.x Make an array containing N zeros and ones
                                      Œ!Q All unique permutations
                                      Œɠ€ Run-length encode on each permutation
                                      2*H Raise each value to power of 2, and halve
                                      P€S Product of each and sum


                                      Take every possible route on a square grid. The number of ways to move L units in one direction as a rook is 2**(L-1). Apply this to every route and sum the number of ways to traverse each route.







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered 2 hours ago









                                      Bubbler

                                      5,999759




                                      5,999759






























                                           

                                          draft saved


                                          draft discarded



















































                                           


                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function () {
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f176646%2fpartitioning-the-grid-into-triangles%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

                                          How did Captain America manage to do this?

                                          迪纳利

                                          南乌拉尔铁路局