The following should work; I haven't had the guts to run it on your real data yet, but my mini-testset seems to result in an answer...

#!/usr/bin/perl =cut my @t = ( '18.930167', '17.967469', '0.008720', '0.008720', '122.640000', '12493.320000', '359.520000', '288.700000', '359.520000', '89.880000', '32.960000', '56.920000', '13.470000', '1231.360000', '20587.440000', '359.520000', '792.170000', '629.160000', '972.290000', ); my $target = 843.24; =cut my @t = (100,2,32,4,65); my $target = 7; my @terms = _test($target,@t); for my $n (0..$#terms) { next unless $terms[$n]; print (($terms[$n]<0)?'subtract':'add'); print " value number ",$n+1," (",$t[$n],")"; print "\n"; } print "to obtain total of $target\n"; sub _test { my ($target,$number,@rest) = @_; for my $term (0,$number,-$number) { if(@rest==0) { next unless $target == sprintf('%.2f',$sum+$te +rm); return ($term/$number) } my @terms = _test(sprintf('%.2f',$target-$term),@rest) +; next unless @terms; return (($term/$number),@terms); } return; }

It's a recursive algorithm that will eventually try every possibility until it finds a working set of terms... It will probably take a long time with the real dataset, so you'll want to eliminate as many terms as possible beforehand.


In reply to Re: Reverse engineering a formula... by Gilimanjaro
in thread Reverse engineering a formula... by devnul

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.