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]
36 Answers 36
-
\$\begingroup\$ Also works in Python 3 with the replacement of
/with//\$\endgroup\$Nick Kennedy– Nick Kennedy2019年05月30日 18:06:40 +00:00Commented May 30, 2019 at 18:06
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.
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]
-
3\$\begingroup\$ Wait... is that divmod? Clever! And I was struggling with
p... \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2019年05月30日 17:43:41 +00:00Commented May 30, 2019 at 17:43
R, 33 bytes
function(n,x,z=1:x-1)z%%n+z%/%n+1
Ports Jonathan Allan's Python solution.
R, 36 bytes
function(n,x)outer(1:n,0:x,"+")[1:x]
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).
J, (削除) 13 (削除ここまで) 12 bytes
[$[:,1++/&i.
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 rangesi., that is, the left arg becomes0 1 2 3 4 5 6 7and the right arg becomes0 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 91 +: 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
-
1\$\begingroup\$ Nice! My 12-byte alternative \$\endgroup\$Galen Ivanov– Galen Ivanov2019年05月31日 08:43:56 +00:00Commented May 31, 2019 at 8:43
Perl 6, 18 bytes
{(1..*X+ ^*)[^$_]}
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\$.
Try it online or verify all test cases.
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∍εN1÷+
First input is \$n\$, second input is \$x\$.
Try it online or verify all test cases.
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:
N1÷ # 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)
Haskell, 31 bytes
n#x=take x$[1..n]++map(+1)(n#x)
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.
Octave, 25 bytes
@(n,x)((1:n)'+(0:x))(1:x)
Anonymous function that inputs numbers n and x, and outputs a row vector.
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
({}[()]<
(<>[]({}())<>)
>)
>)
}
Forth (gforth), 34 bytes
: f 0 do i over /mod + 1+ . loop ;
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
+ 1+ \ add them together and add 1
. \ output result
loop \ end the counted loop
; \ end the word definition
MATL, (削除) 16 (削除ここまで), 10 bytes
:!i:q+2G:)
-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]
+ % Add these two arrays with broadcasting
2G % Push x again
:) % Take the first x elements
Japt -m, (削除) 12 (削除ここまで) 7 bytes
Port of Jonathan's Python solution.
Takes x as the first input.
%VÄ+UzV
-
\$\begingroup\$ Using
alertorprintinstead of return an Array may reduce this to 34 bytes:n=>g=x=>x&&print(g(--x)|1+x%n+x/n)\$\endgroup\$tsh– tsh2019年05月31日 02:09:00 +00:00Commented May 31, 2019 at 2:09
APL (Dyalog Unicode), (削除) 8 (削除ここまで) 10 bytesSBCS
⊢↑∘∊⊣,/∘⍳⌈
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++\⍥⍳
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
Charcoal, 18 bytes
NθFN⊞υ⊕⎇‹ιθι§υ±θIυ
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:
Nθ Input `n` into variable
N Input `x`
F 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
Iυ Cast list to string for implicit output
-
\$\begingroup\$ 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()). \$\endgroup\$Shaggy– Shaggy2019年05月31日 07:35:14 +00:00Commented May 31, 2019 at 7:35
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));}
APL+WIN, (削除) 29 23 (削除ここまで) 16 bytes
x↑,1+(⍳x←⎕)∘.+⍳⎕
Index origin = 0 and prompts for n and x
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");
}
}
-
2\$\begingroup\$ 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,argvandranges). Also, no need to add any warning messages.. You can assume the input is valid, unless the challenge says otherwise. \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2019年05月31日 12:50:57 +00:00Commented May 31, 2019 at 12:50
C# (Visual C# Interactive Compiler), 41 bytes
x=>n=>new int[x].Select((_,a)=>a/n+a%n+1)