Best
- To: mathgroup at smc.vnet.net
- Subject: [mg28225] Best
- From: Domi <doud25 at caramail.com>
- Date: Thu, 5 Apr 2001 03:00:39 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
Hello Mathematica experts out there,
Sometimes, we can choose some books "free" less than 400FF in a special
offer, in a "cadeau de bienvenue".
<< DiscreteMath`Combinatorica`
(* the prize of possible books choice in a list*)
prix = Sort[{159, 139, 149, 99, 139, 160, 139, 149, 135, 149, 145, 99,
180, 129, 138, 119, 180, 145, 139, 139, 150, 119, 139, 120, 159, 159,
109,170}];
(* choice of 3 or 4 books *)
choix = Union[Union[KSubsets[prix, 4], KSubsets[prix, 3]]];
add[l_] := If[(s = Apply[Plus, l]) < 450 , If[s > 398, {l, {s}}, {}]
, {}];
possibles = Drop[Union[Map[add, choix]], 1];
prix = Sort[
Table[
{possibles[[i, 2]], possibles[[i, 1]]}, {i, 1,
Length[possibles]}]]
{{{399}, {99, 120, 180}}, {{399}, {109, 120, 170}}, {{399}, {109, 145,
145}}, {{399}, {119, 120, 160}}, {{399}, {119, 135, 145}}, {{399},
{120,129, 150}}, {{400}, {120, 135, 145}}, {{402}, {119, 138,145}},
{{402}, {129, 135, 138}}....
The solution for 3 "free" books is = 120 + 135 + 145 = 400
The less expensive for 4 books is = {426}, {99, 99, 109, 119}
Question: Is there a more elegant way to do that?
I would like resolve this inequation : min <= x1 + x2 + ...+ Xn <= max
where x1...xn can take some discret value in a set {v1,v2,..,vk}.
Regards from Domi.
(sorry for my so bad/poor english)