0

The example will work faster: I have some numbers (ie: 2, 3 and 7) and must combine them with any operations (+, -, * and /), so I approximate as much as possible to a given number.

So, for a target number 21, it would find that 3 x 7 is the best option.

For a target of 19, it would find (3 x 7) - 2.

For a target of 16, it would find (2 x 7) + 3 = 17 (best approximation, as exact number couldn't be computed).

If you know of any existing algorithm, it would be highly appreciated some clue ;-)

The target number can be any (let's say up to 100,000) and the operands no more than 10 (you can use each of them just one time).

I know I can use a brute-force algorithm (backtracking), taking out illegal operations (ie, negative numbers), but I need the operation done in less than a second or so, using Javascript, and doesn't look like feasible far away from 6 digits (too much permutations & operations).

Robin Green
  • 32,079
  • 16
  • 104
  • 187
julifos
  • 142
  • 1
  • 2
  • 10
  • For 16, what's wrong with `2x2x2x2`? or `(2x7)+2`? – twalberg Dec 17 '13 at 21:04
  • They are "single" numbers, I can't use as much 2's as I wish, just one, as well as one 3 and one 7 (in the example). – julifos Dec 19 '13 at 09:34
  • If that's the case, then the suggestion that the target number can be anything up to 100,000 is a bit of a misleading statement, as with your example, you'll never be able to do anything higher than 2*3*7=42... As well, suggesting you can have up to 10 operands, when you give an example with 3 numbers, gives the impression that numbers can be used more than once. You might consider rewording to make it a bit more clear... – twalberg Dec 19 '13 at 14:02
  • Edited! You first define a random num from 1 to 100,000 (for example), then some random operands (ie, 2, 3, 7). So, for a target 99,847 with operands 2, 3 and 7, 2x3x7=42 would be OK ;-) – julifos Dec 22 '13 at 10:31

0 Answers0