1

I'm trying to design an algorithm for evenly fitting decimal numbers into a decimal number.

Say I have a set $N$ of decimal numbers ($N_1$, $N_2$, ..., $N_n$) and a target value $Y$. I'd like to see what combination of values of $N$ (values may be used zero to many times) add up evenly to $Y$.

I've accomplished it by selecting random values of $N$ until I either add up to $Y$ or pass it - at which point I start over. This algorithm does not seem efficient, however. Sometimes it takes 50k passes before it lands on an answer.

The set I'm actually working with is 21 dollar values between \$0.89 and \$8.04 and the target is \$43.94.

Is there a simpler algorithm?

1 Answers1

1

Provided the $N_i$ are all positive, this is pretty much exactly the problem of "how many ways can I make value $Y$ out of coins with values $N_i$ (assuming no restriction on the number of coins)?"

That problem has a very easy recursive solution that breaks into two cases at each step:

  1. Include a "coin" of value $N_i$ and recurse.
  2. Assume you have used $N_i$ as many times as you are willing and recurse using only values $N_j$ for $j > i$.
Sebastian
  • 131
  • 3