Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: Reverse engineering a formula...

by Gilimanjaro (Hermit)
on Jun 13, 2006 at 12:54 UTC ( #555025=note: print w/replies, xml ) Need Help??

in reply to Reverse engineering a formula...

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.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://555025]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2022-08-15 12:56 GMT
Find Nodes?
    Voting Booth?

    No recent polls found