Split number into sum components

后端 未结 9 2184
-上瘾入骨i
-上瘾入骨i 2021-02-12 18:07

Is there an efficient algorithm to split up a number into N subsections so that the sum of the numbers adds up to the original, with a base minimum? For example, if

9条回答
  •  一个人的身影
    2021-02-12 18:42

    Well I've come up with something "just for fun".

    It goes incrementally from minimum to number and populates an array with N sections using modulo and random.

    See the jsFiddle here.

    It won't work as expected if there are too many sections for this number. (ie number < N(N+1)/2)

提交回复
热议问题