Steve C.

asked • 01/08/24

Algorithm to find whole number sequences summing to a specific value

I need an efficient general algorithm to calculate all the sequences of N whole numbers that sum to a given whole number, M. There are (M+N-1)!/[M!*(N-1)!] such sequences. For example, if N = 3 and M = 4 then

(M+N-1)!/[M!*(N-1)!] = 15, and those 15 sequences are

(0,0,4), (0,1,3), (0,2,2), (0,3,1), (0,4,0),

(1,0,3), (1,1,2), (1,2,1), (1,3,0), (2,0,2),

(2,1,1), (2,2,0), (3,0,1), (3,1,0), (4,0,0).

Ideally the algorithm would be given the first of these sequences and would then generate each successive sequence from the previous one, but that is not a necessary requirement.

1 Expert Answer

By:

Abbos N. answered • 01/10/24

Tutor
New to Wyzant

Full Stack Developer

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.