In the test case, not enough input variables.
Unless I am misunderstanding the question, the correct result for x=7 and n=3 is y_correct=21, not 66.
The author's problem seems to be wrong. I am pretty sure that there are 105 possible triplets that add to 7 within the required range (if order matters). See explanation bellow:
3 positive numbers that add to 7: nchoosek(6,2) (apply the stars and bars theorem on a set with 7 ones)
0 and 2 positive numbers that add to 7: nchoosek(6,1) (pick 0 and apply the stars and bars theorem) times 3 since 0 can be at 3 positions
-1 and 2 positive numbers that add to 8: nchoosek(7,1) (pick -1 and apply the stars and bars theorem) times 3 since -1 can be at 3 positions
-2 and 2 positive numbers that add to 9: nchoosek(8,1) (pick -2 and apply the stars and bars theorem) times 3 since -2 can be at 3 positions
And finally we have the unique triplets:
[0, 0, m1], [0,-1,m2], [0,-2,m3], [-1,-1,m4], [-1,-2,m5], [-2,-2,m6] in which each mi has only one possible value. Where a number repeats there are 3 possibilities and where none repeat 6.
Adding every case results in 105 possible triplets, not 66.
I've also discovered that for some reason nchoosek(12,2)=66 (which alledgedly would be the way to obtain 7 summing 3 numbers), and that nchoosek(79,3)=79079 (which alledgedly would be the way to obtain 40 summing 4 numbers, but I really doubt that's true: following the same reasoning described above we obtain a different number).
PS: It seems that only the first case of the test suite is correct. The 3 pairs for obtaining 1 are [0,1],[-1,2] and [-2,3].
Silly me, forgot we could just generate them all at a computer, and as William said it, there are 21 unique triples that add to 7 within the required range (if order does not matter), namely:
x =
-2 -2 11
-2 -1 10
-2 0 9
-2 1 8
-2 2 7
-2 3 6
-2 4 5
-1 -1 9
-1 0 8
-1 1 7
-1 2 6
-1 3 5
-1 4 4
0 0 7
0 1 6
0 2 5
0 3 4
1 1 5
1 2 4
1 3 3
2 2 3
Find common elements in matrix rows
1566 Solvers
Back to basics 25 - Valid variable names
297 Solvers
Make a random, non-repeating vector.
4090 Solvers
125 Solvers
122 Solvers
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!