Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[euler/prob029] bikeshedding and benchmarking
  • Loading branch information
gerdr committed Oct 25, 2012
1 parent dc828c7 commit dc602bd
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions euler/prob029-gerdr.pl
@@ -1,10 +1,14 @@
use v6;

# same algorithm as polettix' solution
# compute number of unique powers a**b with bases \a in range 2..A
# and exponents \b in range 2..B

sub count-unique-powers(Int \A, Int \B --> Int) {
# computes number of unique powers a**b with bases \a in range 2..A
# and exponents \b in range 2..B
sub count-naively(Int \A, Int \B --> Int) {
+(2..A X=> 2..B).classify({ .key ** .value })
}

sub count-smartly(Int \A, Int \B --> Int) {
# uses the same algorithm as polettix' solution

my %seen-bases;
my $seen-values = [+] gather {
Expand All @@ -21,7 +25,7 @@

# mark powers of \base according to their exponent
# relative to \root
%seen-exponents{(2..B).map(* * exp)} = True...*;
%seen-exponents{(2..B) >>*>> exp} = True xx *;

# avoid double-counting
%seen-bases{base} = True;
Expand All @@ -36,11 +40,17 @@
(A - 1 - %seen-bases) * (B - 1) + $seen-values
}

sub bench(|) {
my \start = now;
my \result = callsame;
my \end = now;
return result, round (end - start) * 1000;
}

sub MAIN(Int $A = 100, Int $B = 100, Bool :$verify) {
say 'got ',
count-unique-powers $A, $B;
&count-smartly.wrap(&bench);
&count-naively.wrap(&bench);

say 'expected ',
+(2..$A X=> 2..$B).classify({ .key ** .value })
if $verify;
printf "got %u [%ums]\n", count-smartly $A, $B;
printf "expected %u [%ums]\n", count-naively $A, $B if $verify;
}

0 comments on commit dc602bd

Please sign in to comment.