diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2019-04-13 05:40:19 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-04-13 05:40:19 +0100 |
| commit | 7f605d60af5251ecca9222686d9fa6cb94c43979 (patch) | |
| tree | 80605e40e81209c944869970477b909578edabb3 | |
| parent | 8b880c7d6af0196945a1fbc84dd5f5f56cd0a683 (diff) | |
| parent | 081b96b81df4e12870d90d905db8aaf7f0bbaed8 (diff) | |
| download | perlweeklychallenge-club-7f605d60af5251ecca9222686d9fa6cb94c43979.tar.gz perlweeklychallenge-club-7f605d60af5251ecca9222686d9fa6cb94c43979.tar.bz2 perlweeklychallenge-club-7f605d60af5251ecca9222686d9fa6cb94c43979.zip | |
Merge pull request #47 from fjwhittle/master
| -rw-r--r-- | challenge-003/fjwhittle/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-003/fjwhittle/perl6/ch-1.p6 | 30 |
2 files changed, 11 insertions, 20 deletions
diff --git a/challenge-003/fjwhittle/blog.txt b/challenge-003/fjwhittle/blog.txt new file mode 100644 index 0000000000..8fff3f1ae1 --- /dev/null +++ b/challenge-003/fjwhittle/blog.txt @@ -0,0 +1 @@ +http://rage.powered.ninja/2019/04/hamming-it-up-in-perl-6-weekly.html diff --git a/challenge-003/fjwhittle/perl6/ch-1.p6 b/challenge-003/fjwhittle/perl6/ch-1.p6 index 25785ed2e2..0232dfceba 100644 --- a/challenge-003/fjwhittle/perl6/ch-1.p6 +++ b/challenge-003/fjwhittle/perl6/ch-1.p6 @@ -11,34 +11,24 @@ unit sub MAIN( ); # 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; +my $smooth5 = gather { + # Initialize some iterators. + my ($i2, $i3, $i5) := ($smooth5.iterator for ^3); + my ($n2, $n3, $n5) := 1 xx 3; # 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; + take my $n := ($n2, $n3, $n5).min; - # 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; + # Advance the iterators that matched. + $n2 == $n and $n2 := $i2.pull-one * 2; + $n3 == $n and $n3 := $i3.pull-one * 3; + $n5 == $n and $n5 := $i5.pull-one * 5; } } -@smooth5.[^$count].say; +$smooth5.[^$count].say; for @print -> $n { ($n.fmt('%7d') ~ ': ' ~ @smooth5[$n-1]).say; |
