# Incremental Ranges!

Your task is to, given two positive integers, $$\x\$$ and $$\n\$$, return the first $$\x\$$ numbers in the incremental ranges sequence.

The incremental range sequence first generates a range from one to $$\n\$$ inclusive. For example, if $$\n\$$ was $$\3\$$, it would generate the list $$\[1,2,3]\$$. It then repeatedly appends the last $$\n\$$ values incremented by $$\1\$$ to the existing list, and continues.

An input of $$\n=3\$$ for example:

n=3
1. Get range 1 to n. List: [1,2,3]
2. Get the last n values of the list. List: [1,2,3]. Last n=3 values: [1,2,3].
3. Increment the last n values by 1. List: [1,2,3]. Last n values: [2,3,4].
4. Append the last n values incremented to the list. List: [1,2,3,2,3,4]
5. Repeat steps 2-5. 2nd time repeat shown below.

2nd repeat:
2. Get the last n values of the list. List: [1,2,3,2,3,4]. Last n=3 values: [2,3,4]
3. Increment the last n values by 1. List: [1,2,3,2,3,4]. Last n values: [3,4,5].
4. Append the last n values incremented to the list. List: [1,2,3,2,3,4,3,4,5]


Test cases:

n,   x,   Output
1,  49,   [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49]
2, 100,   [1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13,14,14,15,15,16,16,17,17,18,18,19,19,20,20,21,21,22,22,23,23,24,24,25,25,26,26,27,27,28,28,29,29,30,30,31,31,32,32,33,33,34,34,35,35,36,36,37,37,38,38,39,39,40,40,41,41,42,42,43,43,44,44,45,45,46,46,47,47,48,48,49,49,50,50,51]
3,  13,   [1,2,3,2,3,4,3,4,5,4,5,6,5]


# Python 2, 39 bytes

lambda n,x:[v/n+v%n+1for v in range(x)]


Try it online!

• Also works in Python 3 with the replacement of / with // – Nick Kennedy May 30 '19 at 18:06

# R, 33 bytes

function(n,x,z=1:x-1)z%%n+z%/%n+1


Try it online!

# R, 36 bytes

function(n,x)outer(1:n,0:x,"+")[1:x]


Try it online!

My original solution; generates an $$\n\times x\$$ matrix with each column as the increments, i.e., $$\1 \ldots n, 2\ldots n+1,\ldots\$$, then takes the first $$\x\$$ entries (going down the columns).

# Jelly, 4 bytes

Ḷd§‘


A dyadic Link accepting two positive integers, x on the left and n on the right, which yields a list of positive integers.

Try it online!

### How?

Ḷd§‘ - Link: x, n              e.g   13, 3
Ḷ    - lowered range (x)             [0,1,2,3,4,5,6,7,8,9,10,11,12]
d   - divmod (n)                    [[0,0],[0,1],[0,2],[1,0],[1,1],[1,2],[2,0],[2,1],[2,2],[3,0],[3,1],[3,2],[4,0]]
§  - sums                          [0,1,2,1,2,3,2,3,4,3,4,5,4]
‘ - increment (vectorises)        [1,2,3,2,3,4,3,4,5,4,5,6,5]

• Wait... is that divmod? Clever! And I was struggling with p... – Erik the Outgolfer May 30 '19 at 17:43

# 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  • – Galen Ivanov May 31 '19 at 8:43 # Perl 6, 18 bytes {(1..*X+ ^*)[^$_]}


Try it online!

Curried function f(x)(n).

### Explanation

{                }  # Anonymous block
X+     # Cartesian product with addition
1..*       # of range 1..Inf
^*  # and range 0..n
(         )[^$_] # First x elements  # 05AB1E, 6 bytes L<s‰O>  Port of @JonathanAllan's Jelly answer, so make sure to upvote him! First input is $$\x\$$, second input is $$\n\$$. Explanation: L # Push a list in the range [1, (implicit) input] # i.e. 13 → [1,2,3,4,5,6,7,8,9,10,11,12,13] < # Decrease each by 1 to the range [0, input) # → [0,1,2,3,4,5,6,7,8,9,10,11,12] s‰ # Divmod each by the second input # i.e. 3 → [[0,0],[0,1],[0,2],[1,0],[1,1],[1,2],[2,0],[2,1],[2,2],[3,0],[3,1],[3,2],[4,0]] O # Sum each pair # → [0,1,2,1,2,3,2,3,4,3,4,5,4] > # And increase each by 1 # → [1,2,3,2,3,4,3,4,5,4,5,6,5] # (after which the result is output implicitly)  My own initial approach was 8 bytes: LI∍εN¹÷+  First input is $$\n\$$, second input is $$\x\$$. Explanation: L # Push a list in the range [1, (implicit) input] # i.e. 3 → [1,2,3] I∍ # Extend it to the size of the second input # i.e. 13 → [1,2,3,1,2,3,1,2,3,1,2,3,1] ε # Map each value to: N¹÷ # The 0-based index integer-divided by the first input # → [0,0,0,1,1,1,2,2,2,3,3,3,4] + # Add that to the value # → [1,2,3,2,3,4,3,4,5,4,5,6,5] # (after which the result is output implicitly)  # Octave, 25 bytes @(n,x)((1:n)'+(0:x))(1:x)  Anonymous function that inputs numbers n and x, and outputs a row vector. Try it online! ## How it works Consider n=3 and x=13. The code (1:n)' gives the column vector 1 2 3  Then (0:x) gives the row vector 0 1 2 3 4 5 6 7 8 9 10 11 12 13  The addition (1:n)'+(0:x) is element-wise with broadcast, and so it gives a matrix with all pairs of sums: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 2 3 4 5 6 7 8 9 10 11 12 13 14 15 3 4 5 6 7 8 9 10 11 12 13 14 15 16  Indexing with (1:x) retrieves the first x elements of this matrix in column-major linear order (down, then across), as a row vector: 1 2 3 2 3 4 3 4 5 4 5 6 5  # Brain-Flak, 100 bytes (<>)<>{({}[()]<(({}))((){[()](<{}>)}{}){{}{}<>(({})<>)(<>)(<>)}{}({}[()]<(<>[]({}())[()]<>)>)>)}{}{}  With comments and formatting: # Push a zero under the other stack (<>)<> # x times { # x - 1 ({}[()]< # Let 'a' be a counter that starts at n # Duplicate a and NOT (({}))((){[()](<{}>)}{}) # if a == 0 { # Pop truthy {} <> # Reset n to a (({})<>) # Push 0 to each (<>)(<>) } # Pop falsy {} # Decrement A, add one to the other stack, and duplicate that number under this stack ({}[()]< (<>[]({}())<>) >) >) }  Try it online! # Haskell, 31 bytes n#x=take x$[1..n]++map(+1)(n#x)


Try it online!

This might be my favorite kind of recursion. We start with the values from 1 to n and then concatenate those same values (via self-reference) +1. then we just take the first x values.

# Forth (gforth), 34 bytes

: f 0 do i over /mod + 1+ . loop ;


Try it online!

### Code Explanation

: f            \ start a new word definition
0 do         \ start a loop from 0 to x-1
i          \ put the current loop index on the stack
over       \ copy n to the top of the stack
/mod       \ get the quotient and remainder of dividing i by n
.          \ output result
loop         \ end the counted loop
;              \ end the word definition


# MATL, 16, 10 bytes

:!i:q+2G:)


Try it online!

-6 bytes saved thanks to Guiseppe and Luis Mendo!

Explanation:

:!          % Push the array [1; 2; ... n;]
i:q       % Push the array [0 1 2 ... x - 1]
2G    % Push x again
:)  % Take the first x elements

• @LuisMendo Thanks! Clearly, I'm pretty rusty with my MATL :) – James May 31 '19 at 15:48

# Gaia, 8 bytes

…@┅+‡t_<


Try it online!

Does basically the same thing as the Octave and MATL answers.

# Jelly, 5 bytes

+þẎḣ’


Try it online!

# Ruby, 32 bytes

->n,x{(0...x).map{|i|i/n+i%n+1}}


Try it online!

# Japt-m, 12 7 bytes

Port of Jonathan's Python solution.

Takes x as the first input.

%VÄ+UzV


Try it

# JavaScript, 36 bytes

n=>g=x=>x?[...g(--x),1+x%n+x/n|0]:[]


Try It Online!

# Perl 5-na, 43 bytes

@r=map$_..$_+$F[1]-1,1..$_;say"@r[0..$_-1]"  Try it online! # K (oK), 17 16 bytes {y#,/1_y(1+)\!x}  Try it online! # Stax, 6 bytes ⌐çYæ▄9  Run and debug it Unpacked & explained: rmx|%+^ Full program, implicit input (n, x on stack; n in register X) r Range [0 .. x) m Map: x|% Divide & modulo x + Add quotient and remainder ^ Add 1 Implicit output  # APL (Dyalog Unicode), 8 10 bytesSBCS ⊢↑∘∊⊣,/∘⍳⌈  Try it online! A dyadic train. Left arg = interval (n), right arg = number of terms (x). ⎕IO←1. ### How it works ⊢↑∘∊⊣,/∘⍳⌈ ⍝ Left: interval n, Right: terms x ∘⍳⌈ ⍝ Generate range [1..max(n,x)] ⍝ (Too short array will complain at N-wise reduce) ⊣,/ ⍝ Extract length-n intervals (dyadic N-wise reduce) ∘∊ ⍝ Flatten and list the elements into a vector ⊢↑ ⍝ Take first x terms from the above  Porting Jonah's J answer would be 10 bytes in Extended (traditional APL doesn't have an equivalent to J's &): # APL (Dyalog Extended), 10 bytes ⊣↑∘,1++\⍥⍳  Try it online! Uses ⎕IO←0. If ⎕IO←1 were used, I'd need to replace 1+ with ¯1+, costing one more byte (instead of saving two bytes). ### How it works ⊣↑∘,1++\⍥⍳ ⍝ Left: x, Right: n ⍥⍳ ⍝ Create 0-based ranges for both args +\ ⍝ Outer product by addition (shortcut of ∘.+) 1+ ⍝ Increment ∘, ⍝ Flatten the matrix into a vector ⊣↑ ⍝ Take first x elements  # Alchemist, 77 bytes _->In_n+In_x x+n+0y+0z->a+Out_a+Out_" "+m+y y+n->n y+0n->z z+m+a->z+n z+0m->a  Try it online! Increments and outputs a counter n times, then subtracts n-1 before repeating. # Charcoal, 18 bytes ＮθＦＮ⊞υ⊕⎇‹ιθι§υ±θＩυ  Try it online! Link is to verbose version of code. I had dreams of seeding the list with a zero-indexed range and then slicing it off again but that was actually 2 bytes longer. Explanation: Ｎθ Input n into variable Ｎ Input x Ｆ Loop over implicit range ι Current index ‹ Less than θ Variable n ⎇ ι Then current index else θ Variable n ± Negated §υ Cyclically indexed into list ⊕ Incremented ⊞υ Pushed to list Ｉυ Cast list to string for implicit output  # JS, 54 bytes f=(n,x)=>Array.from(Array(x),(_,i)=>i+1-(i/n|0)*(n-1))  Try it online! • Welcome to PPCG :) As this isn't a recursive function, you don't need to count the f=. You can save one byte by currying the parameters (n=>x=>) and another by spreading & mapping the array ([...Array(x)].map()). – Shaggy May 31 '19 at 7:35 ## Haskell, 34 33 bytes n#x=take x$do j<-[1..];[j..j+n-1]


Try it online!

# Perl 5, 39 bytes

say 1+int($_/$F[1])+$_%$F[1]for 0..\$_-1


Try it online!

# C (gcc), 49 44 bytes

Using recursion to save some bytes.

f(n,x){x&&printf("%d ",x%n+x/n+1,f(n,--x));}


Try it online!

# APL+WIN, 29 23 16 bytes

 x↑,1+(⍳x←⎕)∘.+⍳⎕


Index origin = 0 and prompts for n and x

Try it online! Courtesy of Dyalog Classic

# C(clang), 843 bytes

#include <stdlib.h>
main(int argc, char* argv[]){
int x,n;
if (argc == 3 && (n = atoi(argv[1])) > 0 && (x = atoi(argv[2])) > 0){
int* ranges = calloc(x, sizeof *ranges);
for (int i = 0; i < x; i++){
if (i < n){
ranges[i] = i+1;
}
else {
ranges[i] = ranges[i-n] + 1;
}
}
printf("[");
for (int j = 0; j < x - 1; j++){
printf("%d",ranges[j]);
printf(",");
}
printf("%d",ranges[x - 1]);
printf("]\n");
free(ranges);
}
else {
printf("enter a number greater than 0 for n and x\n");
}
}

• Hi, welcome to PPCG! This challenge is tagged [code-golf], which means you have to complete the challenge in as few as possible bytes/characters. You can remove loads of whitespaces, and change variable names to single characters in your code (the argc, argv and ranges). Also, no need to add any warning messages.. You can assume the input is valid, unless the challenge says otherwise. – Kevin Cruijssen May 31 '19 at 12:50

# Icon, 48 bytes

procedure f(x,n)
write((0to n)+(1to x))\n&\z
end


Try it online!

# C# (Visual C# Interactive Compiler), 41 bytes

x=>n=>new int[x].Select((_,a)=>a/n+a%n+1)


Try it online!