I have a random number and I would like to divide it into several parts (addends) with conditions that a part can not be more than 20 and the parts must be as close to each other as possible.
For example, if my random number is 41, addends should be 14, 14, 13. If random number is 60 addends should be 20, 20, 20. If random number is 21 addends should be 11 and 10 and so on.
My code is in Ruby (Rails) so I would most appreciate an answer which gives an efficient implementation of this in Ruby, though pseudo-code or other programming languages are welcome as well.
This is what I found for arrays but I really need to do this thing with numbers: "Splitting an array into equal parts in ruby"