diff options
| author | Joelle Maslak <jmaslak@antelope.net> | 2019-04-14 10:40:09 -0600 |
|---|---|---|
| committer | Joelle Maslak <jmaslak@antelope.net> | 2019-04-14 10:40:09 -0600 |
| commit | 01defbf5c53be4bf6a99de1ebe254bc1920cf3ca (patch) | |
| tree | f5ed882bd2ead2854d23223f55782ae312e7692c | |
| parent | 4a8bbb044b285ddbbb1b1d0ca59e5361dff78984 (diff) | |
| download | perlweeklychallenge-club-01defbf5c53be4bf6a99de1ebe254bc1920cf3ca.tar.gz perlweeklychallenge-club-01defbf5c53be4bf6a99de1ebe254bc1920cf3ca.tar.bz2 perlweeklychallenge-club-01defbf5c53be4bf6a99de1ebe254bc1920cf3ca.zip | |
Add Perl6 solutions for Week 3
| -rw-r--r-- | challenge-003/joelle-maslak/perl6/ch-1.p6 | 18 | ||||
| -rw-r--r-- | challenge-003/joelle-maslak/perl6/ch-2.p6 | 22 |
2 files changed, 40 insertions, 0 deletions
diff --git a/challenge-003/joelle-maslak/perl6/ch-1.p6 b/challenge-003/joelle-maslak/perl6/ch-1.p6 new file mode 100644 index 0000000000..d9649489c7 --- /dev/null +++ b/challenge-003/joelle-maslak/perl6/ch-1.p6 @@ -0,0 +1,18 @@ +#!/usr/bin/env perl6 + +use v6; + +my @hamming = (1..∞).grep: { divisors($^num).grep( *.is-prime ).Set ⊆ (2,3,5).Set }; +say "Hamming numbers [0..4]: " ~ @hamming[^5].join(", "); + +sub divisors(Int:D $i -->Array[Int:D]) { + if ($i == 0) { return 0; } + + my $sqrt = sqrt($i).Int; + + my Int:D @divs = grep { $i %% $^div }, 1..$sqrt; + @divs.append: map { Int($i / $^div) }, @divs; + + return @divs; +} + diff --git a/challenge-003/joelle-maslak/perl6/ch-2.p6 b/challenge-003/joelle-maslak/perl6/ch-2.p6 new file mode 100644 index 0000000000..3200dfc2bf --- /dev/null +++ b/challenge-003/joelle-maslak/perl6/ch-2.p6 @@ -0,0 +1,22 @@ +#!/usr/bin/env perl6 + +use v6; + +sub MAIN(Int:D $rows where $rows ≥ 3) { + my @rows; + + for 0..^($rows) -> $i { + my @row; + if ! $i { + @row = 1; + } else { + @row.append: 1; + @row.append: map { @rows[$i-1][$^a-1] + @rows[$i-1][$^a] }, 1..^$i; + @row.append: 1; + } + @rows.push: @row; + + say (" " x $rows - $i) ~ @row.map( { $^a.fmt("%3d") } ).join(" "); + } +} + |
