diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-10-16 13:17:27 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-10-16 13:17:27 +0100 |
| commit | 76769ec086e2a949fd750718332e0c12e75f60ef (patch) | |
| tree | 9c88629fcea078d8942beb7cf7276f85962b6ace /challenge-134 | |
| parent | 0636ecaef056c0bc4e63ce9c121cf33106091164 (diff) | |
| parent | f418944eadc929cf204bf93cfb3ec56bc362c3d1 (diff) | |
| download | perlweeklychallenge-club-76769ec086e2a949fd750718332e0c12e75f60ef.tar.gz perlweeklychallenge-club-76769ec086e2a949fd750718332e0c12e75f60ef.tar.bz2 perlweeklychallenge-club-76769ec086e2a949fd750718332e0c12e75f60ef.zip | |
Merge pull request #5031 from andemark/branch-for-challenge-134
ch-1.raku improved
Diffstat (limited to 'challenge-134')
| -rw-r--r-- | challenge-134/mark-anderson/raku/ch-1.raku | 55 |
1 files changed, 17 insertions, 38 deletions
diff --git a/challenge-134/mark-anderson/raku/ch-1.raku b/challenge-134/mark-anderson/raku/ch-1.raku index b60563f66c..232362cd0a 100644 --- a/challenge-134/mark-anderson/raku/ch-1.raku +++ b/challenge-134/mark-anderson/raku/ch-1.raku @@ -3,47 +3,26 @@ # This program only deals with pandigital numbers up to 9,876,543,210 # (or up to the 3,265,920th) -use Test; -plan 2; +say pandigital(^5); +say pandigital(0, 1000000, 2000000, 3000000, 3265919); -subset pd-index of UInt where * < 3_265_920; - -my $seq := 0, 362879, 362880, { |($^a + 362880, $^b + 362880) } ... *; +sub pandigital(+$arr where 3265920 > .all) +{ + my @polys = |$arr.map(*.polymod(362880)); -is-deeply pandigital(^5), (1023456789, - 1023456798, - 1023456879, - 1023456897, - 1023456978), 'Example 1'; + my $chunks := < 1023456789 2013456789 3012456789 + 4012356789 5012346789 6012345789 + 7012345689 8012345679 9012345678 >; -is-deeply pandigital($seq[^18]), (1023456789, 1987654320, - 2013456789, 2987654310, - 3012456789, 3987654210, - 4012356789, 4987653210, - 5012346789, 5987643210, - 6012345789, 6987543210, - 7012345689, 7986543210, - 8012345679, 8976543210, - 9012345678, 9876543210), 'Edge Cases'; + my @pans = gather for $chunks.head.comb.permutations + { + .take + }[@polys>>[0]]>>.join; -sub pandigital(+$arr where .all ~~ pd-index) -{ - gather + gather for @pans Z @polys { - for 1..9 -> $n - { - for create-list($n).permutations -> $list - { - last if $list[0] == 0; - take $list; - } - } - }[|$arr]>>.join>>.Int + my $div = .tail.tail; + .head .= trans($chunks[0] => $chunks[$div]) if $div; + take .head; + } } - -sub create-list($n) -{ - my $head = ($n, 0); - my $tail = ^10 (-) $head; - |$head, |$tail.keys.sort; -} |
