Getting all possible sums that add up to a given number

前端 未结 6 1632
灰色年华
灰色年华 2020-12-01 07:17

I\'m making an math app for the android. In one of these fields the user can enter an int (no digits and above 0). The idea is to get all possible sums that make this int, w

6条回答
  •  佛祖请我去吃肉
    2020-12-01 07:43

    This is related to the subset sum problem algorithm.

    N = {N*1, (N-1)+1, (N-2)+2, (N-3)+3 .., N-1 = {(N-1), ((N-1)-1)+2, ((N-1)-1)+3..}

    etc.

    So it's a recursive function involving substitution; whether that makes sense or not when dealing with large numbers, however, is something you'll have to decide for yourself.

提交回复
热议问题