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":



  • 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.