interesting. I don't know about an existing module, but here are some ideas for your search algorithm:
Assuming you want x number of elements in array that add up to y
sub searchLoop {
- pass in:
- target number (y)
- number of elements desired (x)
- the array (array)
- y offset number (offset, starts at 0)
- sort array in descending numerical order and store that into a tempo
+rary array that you can mess with.
- remove entries in the temp array that are larger than y
- foreach ( unshift a value off temp array into z) remaining entry:
- if z = y, print z+offset as a solution (or return it if you only n
+eed one solution)
- run searchLoop(y-z, x-1, temp (with the z element removed), offset
++z)
}
This is a very rough sketch, but it should get you started.
- paul
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|