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] add more 6ish versions
- Loading branch information
Showing
2 changed files
with
42 additions
and
0 deletions.
There are no files selected for viewing
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 |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| use v6; | ||
|
|
||
| constant N = 100; | ||
| say +(2..N X=> 2..N).classify({ .key ** .value }); |
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 |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| use v6; | ||
|
|
||
| # same algorithm as polettix' solution | ||
|
|
||
| # range of bases | ||
| constant A = 2..100; | ||
|
|
||
| # range of exponents | ||
| constant B = 2..100; | ||
|
|
||
| my %seen-bases; | ||
| my $seen-values = [+] gather { | ||
|
|
||
| # 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[*-1]; | ||
|
|
||
| for @powers Z 1..* -> \base, \exp { | ||
| next if %seen-bases{base}; | ||
|
|
||
| # mark powers of \base according to their exponent | ||
| # relative to \root | ||
| %seen-exponents{B.map(* * exp)} = True...*; | ||
|
|
||
| # avoid double-counting | ||
| %seen-bases{base} = True; | ||
| } | ||
|
|
||
| take +%seen-exponents; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| # without duplicates, the result would be A * B | ||
| say (A - %seen-bases) * B + $seen-values; |