#K4, 13 12 11 bytes
K4, 13 12 11 bytes
Solution:
(*1?)\[0<;]
Examples:
q)k)(*1?)\[0<;] 100
100 77 30 17 12 2 0
q)k)(*1?)\[0<;] 100
100 37 28 20 2 0
q)k)(*1?)\[0<;] 100
100 77 61 55 53 6 2 0
q)k)(*1?)\[0<;] 100
100 12 1 0
Explanation:
Iterate over expression while x is greater than zero.
(*1?)\[0<;] / the solution
( )\[0<;] / iterate over brackets whilst 0< 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]}\