Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[euler/prob029] fix, merge and add MAIN() to my solution
- Loading branch information
Showing
2 changed files
with
29 additions
and
27 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| } |