Skip to main content
1 of 3
mkst
  • 5.2k
  • 12
  • 19

#K4, 13 12 bytes

Solution:

(*1?)\[{x};]

Examples:

q)k)(*1?)\[{x};] 100
100 51 29 25 15 9 0
q)k)(*1?)\[{x};] 100
100 6 2 0
q)k)(*1?)\[{x};] 100
100 72 13 0
q)k)(*1?)\[{x};] 100
100 9 5 1 0

Explanation:

Iterates over expression while x is not zero.

(*1?)\[{x};] / the solution
(   )\[{x};] / iterate over brackets whilst {x} evaluates true
  1?         / 1 choose (returns a 1-item list)
 *           / take the first

Bonus:

A 14 byte solution that also works in K (oK); this one is an if/else version of the same theme:

{$[x;*1?x;x]}\

Try it online!

mkst
  • 5.2k
  • 12
  • 19