Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[euler/prob029] fix, merge and add MAIN() to my solution
  • Loading branch information
gerdr committed Oct 25, 2012
1 parent 349db40 commit dc828c7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 27 deletions.
4 changes: 0 additions & 4 deletions euler/prob029-gerdr-idiomatic.pl

This file was deleted.

52 changes: 29 additions & 23 deletions euler/prob029-gerdr.pl
@@ -1,40 +1,46 @@
use v6;

# FIXME: incorrect result for non-square input

# same algorithm as polettix' solution

# range of bases
constant A = 2..100;
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

# range of exponents
constant B = 2..100;
my %seen-bases;
my $seen-values = [+] gather {

my %seen-bases;
my $seen-values = [+] gather {
# visit bases which are powers of preceeding ones
for 2..sqrt(A).Int -> \root {
next if %seen-bases{root};

# visit bases which are powers of preceeding ones
for A[0] .. sqrt(A[*-1]).Int -> \root {
next if %seen-bases{root};
my %seen-exponents;
my @powers = root, * * root ...^ * > A;

my %seen-exponents;
my @powers = root, * * root ...^ * > A[*-1];
for @powers Z 1..* -> \base, \exp {
next if %seen-bases{base};

for @powers Z 1..* -> \base, \exp {
next if %seen-bases{base};
# mark powers of \base according to their exponent
# relative to \root
%seen-exponents{(2..B).map(* * exp)} = True...*;

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

# avoid double-counting
%seen-bases{base} = True;
take +%seen-exponents;
}

take +%seen-exponents;
}

# without duplicates, the result would be (A - 1) * (B - 1)
(A - 1 - %seen-bases) * (B - 1) + $seen-values
}

# without duplicates, the result would be A * B
say (A - %seen-bases) * B + $seen-values;
sub MAIN(Int $A = 100, Int $B = 100, Bool :$verify) {
say 'got ',
count-unique-powers $A, $B;

say 'expected ',
+(2..$A X=> 2..$B).classify({ .key ** .value })
if $verify;
}

0 comments on commit dc828c7

Please sign in to comment.