MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Calculating a given total using only given specific values tia sal22

  • To: mathgroup at smc.vnet.net
  • Subject: [mg104320] Re: Calculating a given total using only given specific values tia sal22
  • From: "Nasser M. Abbasi" <nma at 12000.org>
  • Date: Tue, 27 Oct 2009 05:00:35 -0500 (EST)
  • References: <hc38kb$i9r$1@smc.vnet.net>

"Rick T" <ratulloch at gmail.com> wrote in message 
news:hc38kb$i9r$1 at smc.vnet.net...
> Calculating a given total using only given specific values
>
> Greetings All
>
> I'm trying to figure out a way to calculate a given total
> using only given specific values.
>
> Example:
> The total given is 46
> The numbers I can use are 56,38,20,12,4, and 1
> so the numbers it should use and come back with would be highest to
> lowest
> so it should be 38,4,and 4 because
> these will add up to 46.  Is there a name given to this type of
> mathematics?
>
> And does anyone have an example of how to do this?
>
> tia sal22
>

hummm... you say

"so it should be 38,4,and 4"

But 4 occurs one time in the list {56,38,20,12,4, and 1}, so then you want a 
number to possibly be repeated?

Ok.

Since the numbers in the list can be repeated, then _one_ way to solve this 
is to first try to solve the problem using the original list. If this fails 
(i.e. no combination is found which sums to 46), then I duplicated the list, 
and tried again, and so on.

I used Subsets to find all possible combinations, added each comb, and see 
if it totals the number needed. If so, used Position to find its location in 
the Subsets.  I am sure there are many other ways to solve this:

check[list_, n_] := Module[{r, pos},
   combs = Subsets[list];
    pos = Flatten[Position[Total /@ combs, n]];
    If[Length[pos] == 0, pos, combs[[pos]]]
]

list = {56, 38, 20, 12, 4, 1};
n = 46;

r = check[list, n];

While[Length[r] == 0,
             {list = Flatten[Append[list, list]];
             r = check[list, n]}
        ];

Print[r];

{
 {38, 4, 4},
 {4, 38, 4},
 {20, 12, 1, 12, 1},
 {20, 4, 1, 20, 1},
 {20, 1, 20, 4, 1},
 {12, 1, 20, 12, 1}
}

--Nasser 



  • Prev by Date: Re: Two Axis DateListPlot
  • Next by Date: Re: Summing all the elements along a dimension in a matrix
  • Previous by thread: Re: Calculating a given total using only given specific
  • Next by thread: Rotating an Image