Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
355 views
in Technique[技术] by (71.8m points)

libreoffice calc - Looking for math function for finding highest possibile sum

I have a set of 4-digit numbers from which I have to find the largest possible sum consisting of a maximum of 5 items not exceeding 24 thousand but not less than 21 thousand. Even though it sounds trivial and I do it by hand without any problems (the collections are not very numerous) I can't get it over my head to transfer it to a spreadsheet.

@edit Ex. Set of data {4785,4890,5030,5790,6020,3230,4500,5000,4550,2300,5400,5350.....}

question from:https://stackoverflow.com/questions/65887738/looking-for-math-function-for-finding-highest-possibile-sum

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Tools -> Solver works for the values in your example. Here is the spreadsheet I used, with data in cells A2 through A13.

   Data    Chosen Rows Chosen Values   Calculation
   4785    5           5790            24000
   4890    7           3230
   5030    4           5030
   5790    12          5400
   6020    10          4550
   3230                                      
   4500                                      
   5000                                      
   4550                                      
   2300                                      
   5400                                      
   5350                                      
       

The formula for C2 is =INDIRECT("A"&B2), filled down to C6. Then D2 is simply =SUM(C2:C6).

Here are the Solver settings.

solver settings

Also under Options, use the non-linear solver. Specify that variables are integer (this was required even though I added integer as a limiting condition) and non-negative.

EDIT:

every value can be used only once

In that case, enter the following as an array formula in cell B8, explained at https://stackoverflow.com/a/38286032/5100564.

=SUM(1/COUNTIF(B2:B6;B2:B6))

Then add another limiting condition to the solver: $B$8 => 5.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...