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