Next | The Perl Hardware Store | DC.pm Version | 36 |
divide is straightforward but can run very slowly
It tries the same blind alleys repeatedly
Example:
divide(134, [9, 12, 14, 17, 23, 32, 34, 40, 38, 49]);
Finds 6 different ways to combine the first 8 items to total 86.
9 + 12 + 14 + 17 + 34 = 12 + 17 + 23 + 34 = 14 + 17 + 23 + 32, ...
Each time, it investigates divide(48, [38, 49]) in detail
divide(48, [49]) seven times, divide(10, [49]) nine times
divide(48, []) eight times, divide(10, []) fifteen times
One of each is plenty
This is a common problem with hastily-written recursive functions
Next | Copyright © 2003 M. J. Dominus |