32
\$\begingroup\$

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]
\$\endgroup\$
4
  • 1
    \$\begingroup\$ Can the output be unsorted? i.e.: Is [1, 16, 2, 17] an acceptable output? \$\endgroup\$ Jul 26, 2023 at 4:01
  • \$\begingroup\$ No, the output has to be sorted. \$\endgroup\$
    – Lynn
    Jul 26, 2023 at 13:08
  • \$\begingroup\$ Can the output be 0-based? \$\endgroup\$
    – Shaggy
    Aug 3, 2023 at 15:34
  • \$\begingroup\$ No, the output must start with 1. \$\endgroup\$
    – Lynn
    Aug 3, 2023 at 18:38

36 Answers 36

11
\$\begingroup\$

Python, 53 48 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

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

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
\$\endgroup\$
3
  • \$\begingroup\$ Great approach! I see a clever single-byte save but I won't spoil it :) \$\endgroup\$
    – Lynn
    Jul 25, 2023 at 13:28
  • \$\begingroup\$ @Lynn Not sure if this was what you intended, but managed to save a byte :P \$\endgroup\$ Jul 25, 2023 at 13:57
  • 1
    \$\begingroup\$ Nice, that's exactly what I had! \$\endgroup\$
    – Lynn
    Jul 25, 2023 at 14:07
8
\$\begingroup\$

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.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – ngn
    Jul 27, 2023 at 8:28
7
\$\begingroup\$

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).

\$\endgroup\$
6
\$\begingroup\$

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

\$\endgroup\$
6
\$\begingroup\$

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
\$\endgroup\$
6
\$\begingroup\$

C (gcc), 54 bytes

i;f(a,b,m){for(i=0;i<m;i++%(a+b)<a&&printf("%d ",i));}

Try it online!

\$\endgroup\$
0
5
\$\begingroup\$

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.

\$\endgroup\$
5
\$\begingroup\$

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)

\$\endgroup\$
2
  • 1
    \$\begingroup\$ 52 bytes using the mod trick \$\endgroup\$
    – roblogic
    Jul 29, 2023 at 6:30
  • \$\begingroup\$ @roblogic Nice! That's distinctive enough, go ahead and post it as a new answer. \$\endgroup\$ Jul 30, 2023 at 14:57
4
\$\begingroup\$

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
\$\endgroup\$
3
\$\begingroup\$

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.

\$\endgroup\$
3
\$\begingroup\$

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
         +     add
          @     k (number of elements to keep)
                j (number of elements to drop)
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Alternative approach, also 5.5 bytes: +.`/+@$,_<@ \$\endgroup\$ Jul 25, 2023 at 14:19
  • 1
    \$\begingroup\$ @DominicvanEssen You should post that as an answer since it's completely different :) \$\endgroup\$
    – xigoi
    Jul 25, 2023 at 14:27
  • 1
    \$\begingroup\$ Done. Nibbles is currently the most prolifically-used language to solve this task! \$\endgroup\$ Jul 25, 2023 at 14:36
3
\$\begingroup\$

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
\$\endgroup\$
3
\$\begingroup\$

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
\$\endgroup\$
3
\$\begingroup\$

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
\$\endgroup\$
3
\$\begingroup\$

Ruby, 41 37 bytes

-4 bytes thanks to Dingus

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

Attempt This Online!

\$\endgroup\$
2
  • \$\begingroup\$ (0...m).map can be m.times to save 4 bytes. \$\endgroup\$
    – Dingus
    Jul 26, 2023 at 9:42
  • \$\begingroup\$ @Dingus But of course. Thanks! \$\endgroup\$
    – Jordan
    Jul 26, 2023 at 14:18
3
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

Swift, 40 bytes

{k,j,l in(1...l).filter{($0-1)%(k+j)<k}}

Try it on SwiftFiddle!

\$\endgroup\$
2
\$\begingroup\$

><> (Fish), 48 bytes

i:i+i0v/
v   1+>$:@$:@(?;:r:}$:}$r$@%)?
\1+:nao\

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\

enter image description here

\$\endgroup\$
6
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – Lynn
    Jul 25, 2023 at 13:39
  • 2
    \$\begingroup\$ Every time I see a fish answer it looks broken. Using Chrome. Just me? \$\endgroup\$
    – SanguineL
    Jul 25, 2023 at 13:39
  • 1
    \$\begingroup\$ @SanguineL Chrome has a rendering bug, that's why I posted the unformatted version below \$\endgroup\$
    – mousetail
    Jul 25, 2023 at 13:42
  • \$\begingroup\$ @mousetail just wanted to let you know it's broken on Edge and Safari as well. \$\endgroup\$
    – The Thonnu
    Jul 25, 2023 at 13:55
  • 3
    \$\begingroup\$ 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). \$\endgroup\$
    – Lynn
    Jul 25, 2023 at 14:17
2
\$\begingroup\$

Charcoal, 15 12 bytes

I⊕⌕A…⭆²⭆NιN0

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
        N       Next input
       ⭆        Map over implicit range and join
         ι      Outer value
    …           Cyclically extended to length
          N     Third input as a number
  ⌕A            Find all occurrences of
           0    Literal string `0`
 ⊕              Vectorised increment
I               Cast to string
                Implicitly print
\$\endgroup\$
2
\$\begingroup\$

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
\$\endgroup\$
2
\$\begingroup\$

><> (Fish), 43 bytes

ii:i+0v1
)r}:r:<\v?)%{@:&@:$}:&;?
1+:nao^+>

Hover over any symbol to see what it does

In plaintext:

ii:i+0v1
)r}:r:<\v?)%{@:&@:$}:&;?
1+:nao^+>

Try it

control flow

\$\endgroup\$
2
\$\begingroup\$

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

\$\endgroup\$
2
\$\begingroup\$

05AB1E, 8 bytes

LI¹и£ιн˜

Inputs as \$max, [k,j]\$.

Try it online or verify all test cases.

Here an alternative 8 bytes approach:

LDIÅΓ¹∍Ï

Try it online or verify all test cases.

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)
\$\endgroup\$
2
\$\begingroup\$

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!

\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ 42 bytes: (k,j,l)=>0 to(l-1)filter(_%(k+j)<k)map(1+) \$\endgroup\$
    – corvus_192
    Jul 29, 2023 at 9:30
2
\$\begingroup\$

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];}

\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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!

Port of @SanguineL's answer.

\$\endgroup\$
1
\$\begingroup\$

Nim, 60 bytes

proc r(k,j,m:int)=
 for n in 1..m:
  if(n-1)%%(j+k)<k:echo n

Attempt This Online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.