diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2019-04-10 17:23:13 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-04-10 17:23:13 +0100 |
| commit | 766b650778bb183754de50f33b4df354cfef66b6 (patch) | |
| tree | b58c116e4b4b5a78ee04e55fa923169a5acc26b7 | |
| parent | b73d7ce5b93e392f6debad34016cc68cb5340d69 (diff) | |
| parent | 621d4f7037e73f35025a119bd74ef90645f754bf (diff) | |
| download | perlweeklychallenge-club-766b650778bb183754de50f33b4df354cfef66b6.tar.gz perlweeklychallenge-club-766b650778bb183754de50f33b4df354cfef66b6.tar.bz2 perlweeklychallenge-club-766b650778bb183754de50f33b4df354cfef66b6.zip | |
Merge pull request #41 from fjwhittle/master
Week 3 solution by fjwhittle
| -rw-r--r-- | challenge-003/fjwhittle/README | 1 | ||||
| -rw-r--r-- | challenge-003/fjwhittle/perl6/ch-1.p6 | 45 | ||||
| -rw-r--r-- | challenge-003/fjwhittle/perl6/ch-2.p6 | 24 |
3 files changed, 70 insertions, 0 deletions
diff --git a/challenge-003/fjwhittle/README b/challenge-003/fjwhittle/README new file mode 100644 index 0000000000..7dcc95e189 --- /dev/null +++ b/challenge-003/fjwhittle/README @@ -0,0 +1 @@ +Solution by Francis Whittle diff --git a/challenge-003/fjwhittle/perl6/ch-1.p6 b/challenge-003/fjwhittle/perl6/ch-1.p6 new file mode 100644 index 0000000000..25785ed2e2 --- /dev/null +++ b/challenge-003/fjwhittle/perl6/ch-1.p6 @@ -0,0 +1,45 @@ +#!/usr/bin/env perl6 + +use v6; + +subset Count of Int where * > 0; + +#| Script to generate 5-smooth numbers +unit sub MAIN( + Count :n(:$count) = 20, #= How many 5-smooth numbers to generate. + *@print #= specific indices to show. +); + +# Use a lazy list to generate 5-smooth numbers +my @smooth5 = lazy gather { + take 1; # 1 is the first + + # Initialize some iteration counters. + my ($i2, $i3, $i5) = 0 xx 3; + + # I wanted to use actualy Iterators here, but couldn't figure out how to not + # pull elements that didn't exist yet. + + # Generate the next number for each divisor + my $n2 = @smooth5[$i2++] * 2; + my $n3 = @smooth5[$i3++] * 3; + my $n5 = @smooth5[$i5++] * 5; + + # Just keep generating. Does the list become sparse? I don't know! + loop { + # Minimum of the latest iterations + my $n = ($n2, $n3, $n5).min; + take $n; + + # Advance the generators that matched. + $n2 == $n and $n2 = @smooth5[$i2++] * 2; + $n3 == $n and $n3 = @smooth5[$i3++] * 3; + $n5 == $n and $n5 = @smooth5[$i5++] * 5; + } +} + +@smooth5.[^$count].say; + +for @print -> $n { + ($n.fmt('%7d') ~ ': ' ~ @smooth5[$n-1]).say; +} diff --git a/challenge-003/fjwhittle/perl6/ch-2.p6 b/challenge-003/fjwhittle/perl6/ch-2.p6 new file mode 100644 index 0000000000..5149fb1447 --- /dev/null +++ b/challenge-003/fjwhittle/perl6/ch-2.p6 @@ -0,0 +1,24 @@ +#!/usr/bin/env perl6 + +use v6; + +subset Count of Int where * >= 3; + +#| Generates a single row of the triangle. +multi sub generate-row(Int $n) { + my @row = [1]; + + for 1..^$n -> $m { + my $fr = ($n - $m) / $m; + @row.push: @row.tail * $fr; + } + + @row; +} + +#| Generate Pascal's Triangle +sub MAIN( + Count :$rows = 3 #= Number of rows to make (minimum 3). +) { + generate-row($_).put for 1..$rows; +} |
