From 01defbf5c53be4bf6a99de1ebe254bc1920cf3ca Mon Sep 17 00:00:00 2001 From: Joelle Maslak Date: Sun, 14 Apr 2019 10:40:09 -0600 Subject: Add Perl6 solutions for Week 3 --- challenge-003/joelle-maslak/perl6/ch-1.p6 | 18 ++++++++++++++++++ challenge-003/joelle-maslak/perl6/ch-2.p6 | 22 ++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 challenge-003/joelle-maslak/perl6/ch-1.p6 create mode 100644 challenge-003/joelle-maslak/perl6/ch-2.p6 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(" "); + } +} + -- cgit