Skip to main content
added 873 characters in body
Source Link
Jonah
  • 33.8k
  • 4
  • 40
  • 94

J, 13 12 bytes

[$[:,1++/&i.

Try it online!

how

We take x as the left arg, n as the right. Let's take x = 8 and n = 3 for this example:

  • +/&i.: Transform both args by creating integer ranges i., that is, the left arg becomes 0 1 2 3 4 5 6 7 and the right arg becomes 0 1 2. Now we create an "addition table +/ from those two:

       0 1 2
       1 2 3
       2 3 4
       3 4 5
       4 5 6
       5 6 7
       6 7 8
       7 8 9
    
  • 1 +: Add 1 to every element of this table:

       1 2  3
       2 3  4
       3 4  5
       4 5  6
       5 6  7
       6 7  8
       7 8  9
       8 9 10
    
  • [: ,: Flatten it ,:

       1 2 3 2 3 4 3 4 5 4 5 6 5 6 7 6 7 8 7 8 9 8 9 10
    
  • [ $: Shape it $ so it has the same number of elements as the original, untransformed left arg [, ie, x:

       1 2 3 2 3 4 3 4 
    

J, 13 12 bytes

[$[:,1++/&i.

Try it online!

J, 13 12 bytes

[$[:,1++/&i.

Try it online!

how

We take x as the left arg, n as the right. Let's take x = 8 and n = 3 for this example:

  • +/&i.: Transform both args by creating integer ranges i., that is, the left arg becomes 0 1 2 3 4 5 6 7 and the right arg becomes 0 1 2. Now we create an "addition table +/ from those two:

       0 1 2
       1 2 3
       2 3 4
       3 4 5
       4 5 6
       5 6 7
       6 7 8
       7 8 9
    
  • 1 +: Add 1 to every element of this table:

       1 2  3
       2 3  4
       3 4  5
       4 5  6
       5 6  7
       6 7  8
       7 8  9
       8 9 10
    
  • [: ,: Flatten it ,:

       1 2 3 2 3 4 3 4 5 4 5 6 5 6 7 6 7 8 7 8 9 8 9 10
    
  • [ $: Shape it $ so it has the same number of elements as the original, untransformed left arg [, ie, x:

       1 2 3 2 3 4 3 4 
    
added 18 characters in body
Source Link
Jonah
  • 33.8k
  • 4
  • 40
  • 94

J, 1313 12 bytes

[{.[[$[:,1++/&i.

Try it online!Try it online!

J, 13 bytes

[{.[:,1++/&i.

Try it online!

J, 13 12 bytes

[$[:,1++/&i.

Try it online!

Source Link
Jonah
  • 33.8k
  • 4
  • 40
  • 94

J, 13 bytes

[{.[:,1++/&i.

Try it online!