Message Boards Message Boards

0
|
6021 Views
|
1 Reply
|
1 Total Likes
View groups...
Share
Share this post:

Tuples that sum to 1 (for large n)

Posted 9 years ago

Hi! I am trying to find all the possible weights that sum to one:

weights = Table[0.05*i,{i,0,20}] (* list of weights *)
n= 2
alltuples =Tuples[weights,n]; (*generate all tuples *)
Length[alltuples] (* number of tuples there are *)
Select[alltuples, Plus@@#==1&] (*list of all the tuples that sum to one *)

So this works and I get all the permutations that add to 1. However I want this done with n = 9 and n=10. This is not possible as it crashes (due to memory issues). You can try setting n=9 and the code won't work.

Is there a more elegant way to approach this problem? I guess I am using a brute force method which is not appropriate given the size of the problem.

Thanks! Priyan.

POSTED BY: Priyan Fernando
Posted 9 years ago

Hi. You could try using IntegerPartitions[n,k]. eg.,

f[n,k] := IntegerPartitions[n, k]/n

f[20,2] // N

{{1.}, {0.95, 0.05}, {0.9, 0.1}, {0.85, 0.15}, {0.8, 0.2}, {0.75, 0.25}, {0.7, 0.3}, {0.65, 0.35}, {0.6, 0.4}, {0.55, 0.45}, {0.5, 0.5}}

Length[f[20,10]]

530

Regards

POSTED BY: tad
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract