# Make a k-skip-j range

On the Mathematica Stack Exchange, 100xln2 asks:

I need a list of integers […] The list contains integers and is characterized by [three] parameters, lets call them k and j [and listmax], which should be variable.

If k=1 and j=1 then the list would look like this: {1,3,5,7,9,11,....until listmax}

If k=2 and j=3 then the list would look like this: {1,2,6,7,11,12,16,17,.....until listmax}

Generally speaking, the List contains the first k integers, then doesn't contain the next j integers, then contains the next k integers, then doesn't contain the next j integers and so on and so on.

Let's help them out! (I hope they're okay with Jelly, Vyxal, or Befunge code instead of Mathematica…)

Given three positive integers $$\(k, j, \text{listmax})\$$, make a list of integers as described above, sorted in increasing order.

For $$\(3,2,6)\$$ you should return [1,2,3,6] rather than [1,2,3] or [1,2,3,6,7,8]. You can imagine you're generating an infinite “include 3, skip 2” list, and taking elements from it as long as they're ≤ 6.

# Rules

This is . Write the shortest possible answer, measured in bytes.

Standard I/O methods apply, which means valid submissions include:

• a three-parameter function that returns a list of numbers,
• a three-parameter function that prints each number in the list,
• a full program that reads inputs and prints each number in the list,
• and so on.

# Tests

In the format k j listmax --> output:

1 1 11 --> [1, 3, 5, 7, 9, 11]
2 13 19 --> [1, 2, 16, 17]
2 13 16 --> [1, 2, 16]
1 4 49 --> [1, 6, 11, 16, 21, 26, 31, 36, 41, 46]
2 4 22 --> [1, 2, 7, 8, 13, 14, 19, 20]
2 10 13 --> [1, 2, 13]
5 15 10 --> [1, 2, 3, 4, 5]
1 13 27 --> [1, 15]
7 4 31 --> [1, 2, 3, 4, 5, 6, 7, 12, 13, 14, 15, 16, 17, 18, 23, 24, 25, 26, 27, 28, 29]
99 99 1 --> [1]

• Can the output be unsorted? i.e.: Is [1, 16, 2, 17] an acceptable output? Commented Jul 26, 2023 at 4:01
• No, the output has to be sorted.
– lynn
Commented Jul 26, 2023 at 13:08
• Can the output be 0-based? Commented Aug 3, 2023 at 15:34
– lynn
Commented Aug 3, 2023 at 18:38

# Python, 5348 47 bytes

lambda k,j,l:[x+1for x in range(l)if x%(k+j)<k]


Attempt This Online!

-5 by Lynn

-1 by Ethan C

• Nice answer! There's a way you can save a few more bytes. Hint: [x+1for
– lynn
Commented Jul 25, 2023 at 13:43
• @Lynn Knew I was missing something lol, I appreciate it. Commented Jul 25, 2023 at 13:53
• Are the parenthesis around x really necessary? if x%(k+j)<k Commented Jul 25, 2023 at 16:11
• @EthanC You're right. The parenthesis were necessary until Lynn's update, and I didn't realize they could go. Commented Jul 25, 2023 at 18:33

# Jelly, 6 5 bytes

Ø+xṁM


Try it online!

Takes input as [k, j] on the left, and listmax on the right.

-1 byte thanks to Lynn pushing me to save a byte!

## How it works

Ø+xṁM - Main link. Takes [k, j] on the left, listmax on the right
Ø+    - [1, -1]
x   - Repeat 1 k times and -1 j times
ṁ  - Mold this list of length k+j to length listmax
M - Maximal indices, i.e. indices of the 1s

• Great approach! I see a clever single-byte save but I won't spoil it :)
– lynn
Commented Jul 25, 2023 at 13:28
• @Lynn Not sure if this was what you intended, but managed to save a byte :P Commented Jul 25, 2023 at 13:57
• Nice, that's exactly what I had!
– lynn
Commented Jul 25, 2023 at 14:07

# J, 12 11 bytes

>:@I.@$1-I.  Attempt This Online! Takes input as listmax f (k, j). -1 byte thanks to ngn by taking k,j instead of j,k. >:@I.@$1-I.
I.  k copies of 0 and j copies of 1
1-    change 0 to 1 and 1 to 0
$take listmax cyclically I.@ convert each occurrence of 1 to its index (0-based) >:@ add 1  # K (ngn/k), 10 bytes {1+&x#~&y}  Try it online! Takes input as f[listmax;k,j]. Pretty much a built-in-to-built-in translation of the J solution above. • if you swap the arguments j and k, you can replace reverse with not and save a byte in the j solution: |.@I. -> 1-I. – ngn Commented Jul 27, 2023 at 8:28 # R, 32 31 bytes \(k,j,m)which((1:m-1)%%(k+j)<k)  Attempt This Online! Indexes of elements of 0..(listmax-1) that are less than k after modulo-(k+j). # Dyalog APL, 6 bytes Thanks @Lynn for -6 ⍸1=⍴∘⍸­⁡​‎‎⁪⁡⁪⁠⁪⁡⁪‏‏​⁡⁠⁡‌⁢​‎‎⁪⁡⁪⁠⁪⁢⁢⁪‏‏​⁡⁠⁡‌⁣​‎‎⁪⁡⁪⁠⁪⁤⁪‏⁠⁪⁪‏​⁡⁠⁡‌⁤​‎‎⁪⁡⁪⁠⁪⁢⁪‏⁠‎⁪⁡⁪⁠⁪⁣⁪‏‏​⁡⁠⁡‌­ ⍸ # ‎⁡At what indices ⍸ # ‎⁢is the list of k 1's and j 2's ⍴ # ‎⁣cycled to length listmax 1= # ‎⁤equal to 1? 💎  Created with the help of Luminespire. left argument listmax, right argument pair of k and j # Octave / MATLAB, 30 bytes @(k,j,m)find(mod(0:m-1,k+j)<k)  Try it online! ### Explanation @(k,j,m)find(mod(0:m-1,k+j)<k) @(k,j,m) % Define anonymous function of k, j, m 0:m-1 % Range [0 1 2 ... m-1] mod( ,k+j) % Modulo k+j (element-wise) <k % Less than k? (element-wise) find( ) % (1-based) Indices of true entries  # C (gcc), 54 bytes i;f(a,b,m){for(i=0;i<m;i++%(a+b)<a&&printf("%d ",i));}  Try it online! # C (gcc), 62 bytes i,j;f(a,b,m){for(i=j=0;m/++i;j-->b&&printf("%d ",i))j=j?:a+b;}  Try It Online! Ungolfed: i,j; f(a,b,m) { for(i=j=0;m/++i;j-->b&&printf("%d ",i)) j=j?:a+b; }  i is the counter, and the loop ends when it's greater than m, which is the maximum value. j is initialized to 0, but is reinitialized to a+b before it is read from. It's decremented once for each iteration of the loop, and i is only printed when j is greater than b, so only the first a+b - b = a values are printed. When j reaches 0 after b more iterations, it's set to a+b again. # Zsh, 59 bytes for ((;i<$1&&i++<$3;))l+=({$i..$3..$[$1+$2]})
echo ${(on)l}  Attempt This Online! I really think there's some gains to be had from another method, but that method eludes me. This loops, iterating the starting point of a brace expansion {$start..$end..$skip} until it reaches the first or third argument (j or listmax). Each brace expansion is appended to the list l. Finally, the (on) flag sorts the list numerically.

-8 bytes if the output does not need to be sorted. (<<<$l) • 52 bytes using the mod trick Commented Jul 29, 2023 at 6:30 • @roblogic Nice! That's distinctive enough, go ahead and post it as a new answer. Commented Jul 30, 2023 at 14:57 # Husk, 7 bytes ṁ↑³Cḣ⁰+  Try it online! Same approach as my Nibbles answer. Takes arguments in order k,listmax,j. ṁ↑³Cḣ⁰+ C # cut ḣ⁰ # the range from 1..middle argument # into sublists of length + # sum of other arguments ṁ # then map over this list-of-lists ↑ # taking ³ # first-arg elements from each ṁ # and flatten the result  Alternative approach, also 7 bytes f¢Ṙ⁰ḋ2ḣ  Try it online! Takes arguments in order [k,j],listmax. f¢Ṙ⁰ḋ2ḣ Ṙ # repeat ḋ2 # the binary digits of 2 (so: [1,0]) ⁰ # each by number given in arg 1 ¢ # and repeat this list infinitely; f # now use this to filter elements of ḣ # 1..arg 2  # Excel, 68 bytes =LET(a,TOROW(SEQUENCE(,A1)+(A1+B1)*SEQUENCE(C1,,0)),FILTER(a,a<=C1))  k, j and listmax in A1, B1 and C1 respectively. Outputs a horizontal array. # Nibbles, 5.5 bytes |,_-@%-$~+@


Attempt This Online!

Find all numbers $$\n\$$ from $$\1\$$ to $$\m\$$ such that $$\(n-1) \bmod (k+j) < k\$$.

|,_-@%-$~+@ | filter , range _ max number by the following value being strictly positive: - subtract @ k (number of elements to keep) % modulo - ~ subtract 1 from$        the element
@     k (number of elements to keep)
j (number of elements to drop)

• Alternative approach, also 5.5 bytes: +./+@$,_<@ Commented Jul 25, 2023 at 14:19 • @DominicvanEssen You should post that as an answer since it's completely different :) Commented Jul 25, 2023 at 14:27 • Done. Nibbles is currently the most prolifically-used language to solve this task! Commented Jul 25, 2023 at 14:36 # Nibbles, 5.5 bytes (11 nibbles) +./+@$,_<@


Attempt This Online!

Different approach to xigoi's Nibbles answer, but still 5.5 bytes...

+./+@$,_<@ / # make chunks of size +@$      #  sum of first two arguments
,_    # from the range 1..third argument
.           # then, for each chunk
<@  #  take first arg elements
+            # and flatten the list of chunks


# JavaScript (ES6), 46 bytes

Expects (k)(j)(listmax).

k=>j=>g=m=>m--?m%(k+j)<k?[...g(m),m+1]:g(m):[]


Try it online!

### Commented

k =>                 // 1st function taking k
j =>                 // 2nd function taking j
g = m =>             // 3rd recursive function taking m
m-- ?                // if m is not 0 (decrement it afterwards):
m % (k + j) < k ?  //   if m modulo (k + j) is less than k:
[...g(m), m + 1] //     append the result of a recursive call,
//     followed by m + 1
:                  //   else:
g(m)             //     just do a recursive call and append nothing
:                    // else:
[]                 //   stop


# Vyxal, 6 bytes

øḊ⁰ẎT›


Try it Online!

(or 5.5 bytes if you're using vyncode)

øḊ     # Perform dyadic run length decoding - creates a list of
# [[list_max] * k, [0] * j]
⁰Ẏ   # Slice to length list_max
T› # Truthy indices + 1


# Ruby, 41 37 bytes

-4 bytes thanks to Dingus

->k,j,m{m.times{p _1+1if _1%(k+j)<k}}


Attempt This Online!

• (0...m).map can be m.times to save 4 bytes. Commented Jul 26, 2023 at 9:42
• @Dingus But of course. Thanks! Commented Jul 26, 2023 at 14:18

# Minecraft Function, 307 bytes

scoreboard players operation r d = c d
execute if score c d matches 0 run scoreboard players operation j d += k d
scoreboard players operation r d %= j d
scoreboard players add c d 1
execute if score r d < k d run tellraw @a {"score":{"name":"c","objective":"d"}}
execute if score c d < m d run function a:b


Must be run as a function named b in a data pack named a. The function increments c in a loop while c < m. Each iteration, if c % (k + j) < k, it prints c.

k, j, and listmax are input as scoreboard players k, j, and m for objective d. To set up the inputs, run the commands:

/scoreboard objectives add d dummy
/scoreboard players set k d <value>
/scoreboard players set j d <value>
/scoreboard players set m d <value>


The above commands could be considered part of the program, but since they are necessary to provide input they have not been counted.

{k,j,l in(1...l).filter{($0-1)%(k+j)<k}}  Try it on SwiftFiddle! # ><> (Fish), 48 bytes   Hover over any symbol to see what it does Try it Unformatted version of code if above looks broken: i:i+i0v/ v 1+>$:@$:@(?;:r:}$:}$r$@%)?
\1+:nao\


• Cool! I think your explanations would be more compatible if you used <a href="/." title="read a character">i</a> instead of <img> with a broken src.
– lynn
Commented Jul 25, 2023 at 13:39
• Every time I see a fish answer it looks broken. Using Chrome. Just me? Commented Jul 25, 2023 at 13:39
• @SanguineL Chrome has a rendering bug, that's why I posted the unformatted version below Commented Jul 25, 2023 at 13:42
• @mousetail just wanted to let you know it's broken on Edge and Safari as well. Commented Jul 25, 2023 at 13:55
• I wouldn't characterize Chrome's rendering of a "broken image" icon for a broken image as a rendering bug. I believe you're relying on a Firefox quirk (see here and here).
– lynn
Commented Jul 25, 2023 at 14:17

# Charcoal, 15 12 bytes

Ｉ⊕⌕Ａ…⭆²⭆ＮιＮ0


Try it online! Link is to verbose version of code. Would have been 1 fewer byte with 0-indexing. Explanation: Now a port of @lyxal's Vyxal answer.

      ²         Literal integer 2
⭆          Map over implicit range and join
Ｎ       Next input
⭆        Map over implicit range and join
ι      Outer value
…           Cyclically extended to length
Ｎ     Third input as a number
⌕Ａ            Find all occurrences of
0    Literal string 0
⊕              Vectorised increment
Ｉ               Cast to string
Implicitly print


# Arturo, 31 bytes

$[k j l]->select l=>[k>%&-1k+j]  Try it! $[k j l]->          ; a function taking three args
select l=>[     ; select elts in [1..l], assign current elt to &
k>          ; is k greater than...
%&-1        ; current elt minus one modulo...
k+j         ; k plus j?
]               ; end select


# ><> (Fish), 43 bytes





Hover over any symbol to see what it does

In plaintext:

ii:i+0v1
)r}:r:<\v?)%{@:&@:$}:&;? 1+:nao^+>  Try it # Desmos, 36 bytes l=[1...m] f(k,j,m)=l[mod(l-1,k+j)<k]  Uses the same modulo trick that some other answers here have done to filter for the appropriate numbers. Try It On Desmos! Try It On Desmos! - Prettified # 05AB1E, 8 bytes LI¹и£ιн˜  Inputs as $$\max, [k,j]\$$. Here an alternative 8 bytes approach: LDIÅΓ¹∍Ï  Explanation: L # Push a list in the range [1, first (implicit) input max] I # Push the second input-pair [k,j] ¹и # Repeat it the first input amount of times as list £ # Split the first list into parts of those sizes, # containing empty trailing lists if it's out of bounds ι # Uninterleave this list of lists into two parts н # Pop and only keep the first part ˜ # Flatten it # (after which the result is output implicitly)  L # Push a list in the range [1, first (implicit) input max] D # Duplicate this list I # Push the second input-pair [k,j] ÅΓ # Pop the top two lists, and run-length decode it, # resulting in k amount of 1s followed by j amount of 2s ¹∍ # Shorten/extend it to a length equal to the first input max Ï # Only leave the values in the ranged list at the truthy positions # (only 1 is truthy in 05AB1E) # (after which the result is output implicitly)  # Java (JDK), 71 bytes Output is an IntStream if that's acceptable. (k,j,l)->java.util.stream.IntStream.range(1,l+1).filter(i->--i%(k+j)<k)  Try it online! If that's not acceptable, then here's a more classic solution with an array as output (81 bytes) : (k,j,l)->{var r="";for(int i=0;i<l;)r+=i++%(k+j)<k?i+",":"";return r.split(",");}  Try it online! # Scala, 48 bytes (k,j,l)=>for(i<-0 until l if i%(k+j)<k)yield i+1  Try it online! Same modulo trick. • 42 bytes: (k,j,l)=>0 to(l-1)filter(_%(k+j)<k)map(1+) Commented Jul 29, 2023 at 9:30 # Bash+coreutils, 41 bytes seq 1$3|xargs -n$[$1+$2]|cut -d\ -f1-$1


Try it online!

59 bytes: c=$3;for((i=1;i<=c;i+=$1+$2)){ seq$i $[k=i+$1-1,k<c?k:c];}

# Vyxal, 46 bitsv2, 5.75 bytes

₀fẋfẎT›


Try it Online!

Vyncode!!!

## Explained

₀fẋfẎT›­⁡​‎‎⁪⁡⁪⁠⁪⁡⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁪‏⁠‎⁪⁡⁪⁠⁪⁣⁪‏‏​⁡⁠⁡‌⁢​‎‎⁪⁡⁪⁠⁪⁤⁪‏‏​⁡⁠⁡‌⁣​‎‎⁪⁡⁪⁠⁪⁢⁡⁪‏‏​⁡⁠⁡‌⁤​‎‎⁪⁡⁪⁠⁪⁢⁢⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁣⁪‏‏​⁡⁠⁡‌­
₀fẋ      # ‎⁡A list of [[1] * k, [0] * j]
f     # ‎⁢flattened
Ẏ    # ‎⁣with the first listmax items taken, wrapping if needed
T›  # ‎⁤truthy indices + 1
💎


Created with the help of Luminespire.

# Go, 79 bytes

func(k,j,n int)(o[]int){for i:=0;i<n;i++{if i%(k+j)<k{o=append(o,i+1)}}
return}


Attempt This Online!

proc r(k,j,m:int)=