From 06093f2100488062b0a371c8dfb9e05dd2b255fd Mon Sep 17 00:00:00 2001 From: Francis Whittle Date: Thu, 11 Apr 2019 01:29:01 +1000 Subject: Add README for fjwhittle --- challenge-003/fjwhittle/README | 1 + 1 file changed, 1 insertion(+) create mode 100644 challenge-003/fjwhittle/README 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 -- cgit From 507cd92fef765186b1473f86e22efca98af068b2 Mon Sep 17 00:00:00 2001 From: Francis Whittle Date: Thu, 11 Apr 2019 01:33:35 +1000 Subject: fjwhittle: week 3, part 1, perl 6 --- challenge-003/fjwhittle/perl6/ch-1.p6 | 45 +++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 challenge-003/fjwhittle/perl6/ch-1.p6 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; +} -- cgit From 621d4f7037e73f35025a119bd74ef90645f754bf Mon Sep 17 00:00:00 2001 From: Francis Whittle Date: Thu, 11 Apr 2019 02:13:18 +1000 Subject: fjwhittle: week 3, part 2, perl 6 --- challenge-003/fjwhittle/perl6/ch-2.p6 | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 challenge-003/fjwhittle/perl6/ch-2.p6 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; +} -- cgit