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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
This seems to work. Loading the data array for the next recursion seemed, to me, to be the tricky part. I did this by hand and the numbers match. Of course, even with higher math, I still can't add very well ;o)
use strict; use warnings; use diagnostics; #main() { my @data = ([0.11,0.07,0.19], [0.43,0.31,0.37], [0.95,0.78,0.82], [0.91,0.12,0.15], [0.52,0.18,0.32]); foreach (@data){ my $pval = P($_); print "value = $pval\n"; } exit; } #end main() sub P{ my $data = shift; my $n = @{$data}; my $rslt1 = 0; my $rslt2 = 0; my $rslt = 0; return $$data[0] if($n == 1); #r(0) = 0 ==> r(1)-r(0) = r(1) #assume P(r(0)) = 1 unshift @{$data}, 0; for(my $i=1;$i <= $n; $i++){ my @nextdata = (); for (1..$n){ next if($_ == ($n-$i+1)); push @nextdata, $$data[$_]; } # split up rslt1 & 2 for clarity $rslt1 = ($$data[$n-$i+1] - $$data[$n-$i]); $rslt2 = P(\@nextdata); $rslt += $rslt1 * $rslt2; } return $rslt; } __DATA__ value = 0.001595 value = 0.046225 value = 0.549005 value = 0.439894 value = 0.010192

PJ
unspoken but ever present -- use strict; use warnings; use diagnostics; (if needed)

In reply to Re: recursive formula. by periapt
in thread recursive formula. by BioGeek

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



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2024-03-28 18:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found