Easy, peasy. 200 is not that large of a set, although, you can easily push this too far.
As an example, I'll pick some (200) random integers from the set 1:1000 without any repeats. Then I'll find all combinations of three of those numbers that sum to 500.
A = sort(randperm(1000,200));
ind = nchoosek(1:200,3);
sum500ind = find(sum(A(ind),2) == 500);
sum500 = A(ind(sum500ind,:));
The first 10 in the list are:
sum500(1:10,:)
ans =
2 72 426
2 124 374
2 134 364
2 135 363
2 136 362
2 138 360
2 168 330
2 186 312
2 195 303
2 205 293
There are many others of course, 186 in all.
As I said, you can easily overwhelm any such brute fore search.
Or, you can download my partitions code, from the file exchange, and get an answer in one single line of code. It will essentially be in the form of a 0-1 matrix here, indicating which elements of A are to be included in each sum.
p = partitions(500,A,1,3);
size(p)
ans =
186 200
If we allow elements of A to be used more than once, then there are 198 possible solutions in this case.
p = partitions(500,A,[],3);
size(p)
ans =
198 200
0 Comments
Sign in to comment.