Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Cleanups/optimisations.
  • Loading branch information
shlomif committed May 6, 2015
1 parent 212eb22 commit f85b49d
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions categories/euler/prob151-shlomif.p6
Expand Up @@ -3,16 +3,19 @@ use v6;
my $sum = 0;

# rec is short for "recurse".
sub rec($factor, @counts, $result)
# $n is numerator ; $d is denominator
sub rec($n, $d, @counts, $result)
{
say @counts.join(",");
my $cnt = [+] @counts;
if $cnt == 0
{
$sum += $factor * $result;
$sum += $n * $result / $d;
}
else
{
my $new_r = $result + ($cnt == 1);
my $new_d = $d*$cnt;
for (@counts.kv) -> $size, $f
{
if $f > 0
Expand All @@ -23,13 +26,13 @@ sub rec($factor, @counts, $result)
{
@c[$s]++;
}
rec( $factor*$f/$cnt, @c, $result + ($cnt == 1) );
rec( $n*$f, $new_d, @c, $new_r );
}
}
}
return;
}

rec(1,[1],0);
rec(1, 1, [1],0);

say "Result == ", ($sum - 2).fmt("%.6f");

0 comments on commit f85b49d

Please sign in to comment.