MathGroup Archive 2002

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

Search the Archive

need a function for sums of subsets

  • To: mathgroup at
  • Subject: [mg33001] need a function for sums of subsets
  • From: mwganson at (Mark Ganson)
  • Date: Mon, 25 Feb 2002 01:31:17 -0500 (EST)
  • Sender: owner-wri-mathgroup at


I need a function that will find the first subset of a list that will
sum up to a given value.

For example, if I have a list of rationals:

myrationals = {1/2, 1/3, 1/4, 1/8, 3/10, 12/79, 13/38}

I would like a function that will return the first subset of
myrationals that sums up to a given parameter.

We can call the function "findsum".  It would work like so:

In: findsum[myrationals, 3/4]
Out: {1/2, 1/4}

It would be nice also, but not essential, to be able to use another
list as the second parameter and have the function return a list of


In: findsum[myrationals, {3/4, 3/8}]
Out: {{1/2, 1/4}, {1/4, 1/8}}

I need something really fast and memory efficient because my lists
tend to be quite large (up to 100 elements).

Many thanks,

Mark Ganson

  • Prev by Date: Re: Suppression of output...
  • Next by Date: sum problem with infinity
  • Previous by thread: Partial derivatives and implicit inversion?
  • Next by thread: Re: need a function for sums of subsets