diff options
| author | Adam Russell <adam.russell@optum.com> | 2019-04-12 15:44:17 -0400 |
|---|---|---|
| committer | Adam Russell <adam.russell@optum.com> | 2019-04-12 15:44:17 -0400 |
| commit | df86f815d821bdf67f1dc84fd300b23f349871f0 (patch) | |
| tree | 528bdbc2f60ff57c3a3297a2ecf25f188213ec81 /challenge-003 | |
| parent | 8b74c2038a052927add114b4a69f4b7c214e1ad0 (diff) | |
| parent | 8b880c7d6af0196945a1fbc84dd5f5f56cd0a683 (diff) | |
| download | perlweeklychallenge-club-df86f815d821bdf67f1dc84fd300b23f349871f0.tar.gz perlweeklychallenge-club-df86f815d821bdf67f1dc84fd300b23f349871f0.tar.bz2 perlweeklychallenge-club-df86f815d821bdf67f1dc84fd300b23f349871f0.zip | |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-003')
25 files changed, 647 insertions, 1 deletions
diff --git a/challenge-003/alex-daniel/README b/challenge-003/alex-daniel/README new file mode 100644 index 0000000000..65550789bd --- /dev/null +++ b/challenge-003/alex-daniel/README @@ -0,0 +1 @@ +Solution by Alex Daniel. diff --git a/challenge-003/andrezgz/perl5/ch-1.pl b/challenge-003/andrezgz/perl5/ch-1.pl new file mode 100644 index 0000000000..cec1731d72 --- /dev/null +++ b/challenge-003/andrezgz/perl5/ch-1.pl @@ -0,0 +1,17 @@ +#!/usr/bin/perl + +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-003/ +# Challenge #1 +# Create a script to generate 5-smooth numbers, whose prime divisors are less or equal to 5. +# They are also called Hamming/Regular/Ugly numbers. +# For more information, please check this wikipedia. https://en.wikipedia.org/wiki/Regular_number + +use strict; + +my $last = shift || 20; + +foreach my $n ( 1 .. $last ) { + my $r = $n; + for (2,3,5) { $r /= $_ until ($r % $_) } + print $n.$/ if ($r == 1); +} diff --git a/challenge-003/andrezgz/perl5/ch-2.pl b/challenge-003/andrezgz/perl5/ch-2.pl new file mode 100644 index 0000000000..3d9d8e208a --- /dev/null +++ b/challenge-003/andrezgz/perl5/ch-2.pl @@ -0,0 +1,27 @@ +#!/usr/bin/perl + +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-003/ +# Challenge #2 +# Create a script that generates Pascal Triangle. Accept number of rows from the command line. +# The Pascal Triangle should have at least 3 rows. +# For more information about Pascal Triangle, check this wikipedia page. https://en.wikipedia.org/wiki/Pascal%27s_triangle + +use strict; +use warnings; + +my $rows = $ARGV[0] || 0; + +if ($rows < 3) { + print "The Pascal Triangle should have at least 3 rows\n"; + $rows = 3; +} + +my @row = (1); + +while ($rows) { + print join(' ',@row) .$/; + $rows--; + @row = map {$row[$_] += $row[$_+1]} 0..@row-2; + unshift @row, 1; + push @row, 1; +} diff --git a/challenge-003/cliveholloway/README b/challenge-003/cliveholloway/README new file mode 100644 index 0000000000..b69201296e --- /dev/null +++ b/challenge-003/cliveholloway/README @@ -0,0 +1 @@ +Solution by Clive Holloway diff --git a/challenge-003/cliveholloway/perl5/ch-2.pl b/challenge-003/cliveholloway/perl5/ch-2.pl new file mode 100755 index 0000000000..be67bd4673 --- /dev/null +++ b/challenge-003/cliveholloway/perl5/ch-2.pl @@ -0,0 +1,23 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use v5.012; + +my @out=([1],[1,1]); + +for (3..$ARGV[0]) { + my @new_row=(1,@{$out[-1]}); + for (1..$#{$out[-1]}) { + $new_row[$_] = $out[-1][$_-1] + $out[-1][$_]; + } + push @out, \@new_row; +} + +# format data for output - obviously you'll run out of terminal at some point, +# so, this is just a pretty demo output +my $longest_length = length("@{$out[-1]}"); +for (0..$#out) { + $out[$_] = "@{$out[$_]}"; + my $this_length = length($out[$_]); + say ' 'x(($longest_length-$this_length)/2).$out[$_]; +} diff --git a/challenge-003/daniel-mantovani/perl5/ch-1.pl b/challenge-003/daniel-mantovani/perl5/ch-1.pl new file mode 100644 index 0000000000..fa23ce0075 --- /dev/null +++ b/challenge-003/daniel-mantovani/perl5/ch-1.pl @@ -0,0 +1,38 @@ +#!/usr/bin/perl +use strict; +use warnings; +use v5.10; + +my $q = $ARGV[0]; + +die + "Usage: perl $0 <n>\n\nwhere <n> is the (integer) quantity of hamming numbers you ask" + unless $q && $q =~ /^\d+$/; + +my @primes = (2, 3, 5); + +# for each prime, we will define one vector (array), whith just one element [1] +# note that [1] will be first hamming code inside each array! + +my %vec; +$vec{$_} = [1] for @primes; + +for my $i (1 .. $q) { + +# just look for the min value from the head of v2, v3 or v5 +# (without using List::Util, as the challenge encourages no to use external modules) + my $h; + map { $h = $vec{$_}[0] unless $h && $h < $vec{$_}[0] } @primes; + + # and we just got the next hamming number! + say "$i\t", $h; + + for my $f (@primes) { # now for every prime factor + # remove this value on each vector where it is present + shift @{$vec{$f}} if $vec{$f}[0] == $h; + + # and finally calculate and insert new values to each vector + # note that new pushed value is higer than any previous value in each vector + push @{$vec{$f}}, $h * $f; + } +} diff --git a/challenge-003/duncan-c-white/README b/challenge-003/duncan-c-white/README index 81f31dc7c0..d6a6cb2f04 100644 --- a/challenge-003/duncan-c-white/README +++ b/challenge-003/duncan-c-white/README @@ -1 +1,8 @@ -Solution by Duncan C. White +I have investigated Challenge 1 (the Regular numbers) reasonably thoroughly, +building and comparing 4 iterative solutions to generate them. I also +investigated a Lazy List version, storing the tail of the list as a promise - +a function to call when you wanted the next head item, and the returned tail +is another promise - to generate the next item later when needed. However, +I couldn't get that to work so abandoned it:-) + +Challenge 2 (Pascal's Triangle) by contrast is pretty basic and simple-minded. diff --git a/challenge-003/duncan-c-white/perl5/ch-1.pl b/challenge-003/duncan-c-white/perl5/ch-1.pl new file mode 100755 index 0000000000..3e6cf63ed4 --- /dev/null +++ b/challenge-003/duncan-c-white/perl5/ch-1.pl @@ -0,0 +1,220 @@ +#!/usr/bin/perl +# +# iterative generation of the first N regular numbers +# (numbers of the form 2^i.3^j.5^k) +# in ascending order, using 4 different algorithms. +# +# This program takes two optional arguments: +# ch-1-pl [benchmark [firstN]] +# where benchmark defaults to 0, and firstN to 1000. +# - If benchmark is 0, we run each algorithm in turn +# and minimally checks that each algorithm generates +# the same list. +# - If benchmark is any non-zero value (+ or -), each +# algorithm is Benchmarked with the $benchmark value +# as Benchmark::timethese's first parameter. Thus, +# a +ve value means "benchmark this many iterations", +# while a -ve value means "benchmark for abs(this) seconds". +# +# So, for example: +# ./ch-1.pl 0 +# runs all 4 algorithms, telling you whether each result is the same.. +# but: +# ./ch-1.pl -60 +# runs all 4 algorithms repeatedly under Benchmark, and reports: +# Benchmark: running v1, v2, v3, v4 for at least 60 CPU seconds... +# v1: 69 wallclock secs (68.38 usr + 0.00 sys = 68.38 CPU) @ 0.06/s (n=4) +# v2: 64 wallclock secs (63.17 usr + 0.00 sys = 63.17 CPU) @ 26.29/s (n=1661) +# v3: 62 wallclock secs (62.54 usr + 0.00 sys = 62.54 CPU) @ 45.24/s (n=2829) +# v4: 63 wallclock secs (63.38 usr + 0.00 sys = 63.38 CPU) @ 127.90/s (n=8106) + +use strict; +use warnings; + +use Benchmark qw(:all) ; + +# +# my @result = firstn_regnos_v1( $n ); +# Generate the first N regular numbers via basic GENERATE AND TEST: +# generate every integer, then check "is it 2^i.3^j.5^k for any i,j,k"? +# Returns a list of the first N regular numbers. +# +sub firstn_regnos_v1 +{ + my( $n ) = @_; + my @result = (1); + for( my $x=2; @result<$n; $x++ ) + { + my $y=$x; + $y/=5 while $y%5==0; + $y/=3 while $y%3==0; + $y/=2 while $y%2==0; + push @result, $x if $y==1; + } + return @result; +} + + +# +# my @result = firstn_regnos_v2( $n ); +# Generate the first N regular numbers via a todo set +# of higher 2^i.3^j.5^k values that we haven't yet +# checked, and sorting the todo list to pick the smallest +# every time. Returns a list of the first N regular numbers. +# +sub firstn_regnos_v2 +{ + my( $n ) = @_; + my @result = (1); + my %todo = map { $_ => 1 } qw(2 3 5); + for( my $i=1; $i<$n; $i++ ) + { + my @todo = sort { $a <=> $b } keys %todo; + my $next = shift @todo; + push @result, $next; + # update the todo set: next is done, so remove it + delete $todo{$next}; + # update the todo set: add 2*next, 3*nect and 5*next + $todo{2*$next}++; + $todo{3*$next}++; + $todo{5*$next}++; + } + return @result; +} + + +# +# my @result = firstn_regnos_v3( $n ); +# Generate the first N regular numbers via a todo set +# of higher 2^i.3^j.5^k values that we haven't yet +# checked. It's the same as _v2 EXCEPT that we pick the +# smallest element in a different and more efficient way - +# by searching linearly through the todo set - instead of +# forming a list and sorting it, every time. +# Returns a list of the first N regular numbers. +# +sub firstn_regnos_v3 +{ + my( $n ) = @_; + my @result = (1); + my %v = map { $_ => 1 } qw(2 3 5); + for( my $i=1; $i<$n; $i++ ) + { + my $min = each %v; + while( my $v = each %v ) + { + $min = $v if $min>$v; + } + my $next = $min; + push @result, $next; + delete $v{$next}; + $v{2*$next}++; + $v{3*$next}++; + $v{5*$next}++; + } + return @result; +} + + +# +# my @result = firstn_regnos_v4( $firstn ); +# Generate the first $firstn regular numbers via a more +# constrained "todo set" that never has both than +# one term i-j-X or i-X-k or X-j-k in. This is faster +# and uses less memory. +# Returns a list of the first $firstn regular numbers. +# +sub firstn_regnos_v4 +{ + my( $firstn ) = @_; + my @result = (); + my @next = ( [ 1,0,0,0 ] ); # list of N-i-j-k 4-tuples + my %haveij; # distinct "i-j" pairs in @next + my %haveik; # distinct "i-k" pairs + my %havejk; # distinct "j-k" pairs + for( my $a=1; $a<=$firstn; $a++ ) + { + my $tuple = shift @next; + my( $n, $i, $j, $k ) = @$tuple; + #print "debug: result n=$n, i=$i, j=$j, k=$k\n"; + push @result, $n; + # remove i-j-k from have* now that n-i-j-k is done + delete $haveij{"$i-$j"}; + delete $haveik{"$i-$k"}; + delete $havejk{"$j-$k"}; + # consider each of (i+1,j,k), (i,j+1,k) and (i,j,k+1) + foreach my $newt ( [$n*2, $i+1, $j, $k], + [$n*3, $i, $j+1, $k], + [$n*5, $i, $j, $k+1] ) + { + my( $newn, $x, $y, $z ) = @$newt; + #print "debug: found possible next n=$newn, x=$x, y=$y, z=$z\n"; + + # skip newn-x-y-z if we already known x-y-? or x-?-z or ?-x-z + # as this is a higher power combination that we will discover later + next if $haveij{"$x-$y"} || $haveik{"$x-$z"} || $havejk{"$y-$z"}; + + # ok, so newn-x-y-z is genuinely new: add it, first to have* + $haveij{"$x-$y"}++; + $haveik{"$x-$z"}++; + $havejk{"$y-$z"}++; + #print "debug: found actual next n=$newn, x=$x, y=$y, z=$z\n"; + + # second, insert $newn, $x, $y, $z into @next in sorted-by-N order + # - find first pos s.t. next[pos]>newn + my $pos; + for( $pos=0; $pos < @next && $next[$pos]->[0] < $newn; $pos ++ ) + { + } + # - add new item in at pos $pos: + splice( @next, $pos, 0, $newt ); + } + } + return @result; +} + + +# -------------------------------- Main program -------------------------------- + + +my $benchmark = shift // 0; +my $firstn = shift // 1000; + +if( $benchmark ) +{ + timethese( $benchmark, { + 'v1' => sub { firstn_regnos_v1( $firstn ) }, + 'v2' => sub { firstn_regnos_v2( $firstn ) }, + 'v3' => sub { firstn_regnos_v3( $firstn ) }, + 'v4' => sub { firstn_regnos_v4( $firstn ) }, + }); +} else +{ + print "calculating first $firstn regular numbers by v1 (generate and test):\n"; + my @result = firstn_regnos_v1( $firstn ); + my $s1 = join(',', @result); + print "they are: $s1\n"; + + print "calculating first $firstn regular numbers via v2 (todo+sort):\n"; + @result = firstn_regnos_v2( $firstn ); + my $s2 = join(',', @result); + #print "they are: $s2\n"; + + my $same = $s1 eq $s2 ? "same" : "different"; + print "v1 and v2: $same\n"; + + print "calculating first $firstn regular numbers via v3 (todo+find-smallest):\n"; + @result = firstn_regnos_v3( $firstn ); + my $s3 = join(',', @result); + + $same = $s1 eq $s3 ? "same" : "different"; + print "v1 and v3: $same\n"; + + print "calculating first $firstn regular numbers via v4 (clever):\n"; + @result = firstn_regnos_v4( $firstn ); + my $s4 = join(',', @result); + #print "v4: result $s4\n"; + + $same = $s1 eq $s4 ? "same" : "different"; + print "v1 and v4: $same\n"; +} diff --git a/challenge-003/duncan-c-white/perl5/ch-2.pl b/challenge-003/duncan-c-white/perl5/ch-2.pl new file mode 100755 index 0000000000..5109322548 --- /dev/null +++ b/challenge-003/duncan-c-white/perl5/ch-2.pl @@ -0,0 +1,26 @@ +#!/usr/bin/perl +# +# generate the first N rows of Pascal's triangle, no cleverness, just the +# basic formula. + + +use strict; +use warnings; + +use Function::Parameters; + +my $n = shift // 1000; + +my @curr = (1); # current row +my @next; # next row, one element longer than @curr + +for( my $row=1; $row<=$n; $row++ ) +{ + print "row $row: ".join(',',@curr)."\n"; + @next = ( ); + for( my $i=0; $i<=@curr; $i++ ) + { + $next[$i] = ($i==0?0:$curr[$i-1]) + ($i==@curr?0:$curr[$i]); + } + @curr = @next; +} 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; +} diff --git a/challenge-003/nick-logan/perl5/ch-1.pl b/challenge-003/nick-logan/perl5/ch-1.pl new file mode 100644 index 0000000000..b76f7d6d92 --- /dev/null +++ b/challenge-003/nick-logan/perl5/ch-1.pl @@ -0,0 +1,29 @@ +# WARNING: this polyglot breaks best practices of both Perl 5 and Perl 6 in order to run on both + +my @ARGV = do { sub eval { &EVAL(@_) }; eval( ("0" and q|@*ARGS| or q|@ARGV|) ) }; + +my $numbers_tried = 0; +my $numbers_found = 0; + +NUMBERS: while ($numbers_found != @ARGV[0]) { + + $numbers_tried++; + my $state = $numbers_tried; + while ($state != 1) { + if ($state % 2 == 0) { + $state /= 2; + } + elsif ($state % 3 == 0) { + $state /= 3; + } + elsif ($state % 5 == 0) { + $state /= 5; + } + else { + next NUMBERS; + } + } + $numbers_found++; + + print("$numbers_tried\n"); +} diff --git a/challenge-003/nick-logan/perl5/ch-2.pl b/challenge-003/nick-logan/perl5/ch-2.pl new file mode 100644 index 0000000000..10bcd74535 --- /dev/null +++ b/challenge-003/nick-logan/perl5/ch-2.pl @@ -0,0 +1,13 @@ +# WARNING: this polyglot breaks best practices of both Perl 5 and Perl 6 in order to run on both + +my @ARGV = do { sub eval { &EVAL(@_) }; eval( ("0" and q|@*ARGS| or q|@ARGV|) ) }; + +my @state = (1,); +for (1 .. @ARGV[0]) { + print(join(" ", @state), "\n"); + my @row = map &{ sub ($_) { @state[$_] + (@state[$_ + 1] // 0) } }.(), (0 .. ($_ - 2)); + @state = (); + push(@state, 1); + push(@state, $_) for @row; + push(@state, 1); +} diff --git a/challenge-003/nick-logan/perl6/ch-1.p6 b/challenge-003/nick-logan/perl6/ch-1.p6 new file mode 100644 index 0000000000..b76f7d6d92 --- /dev/null +++ b/challenge-003/nick-logan/perl6/ch-1.p6 @@ -0,0 +1,29 @@ +# WARNING: this polyglot breaks best practices of both Perl 5 and Perl 6 in order to run on both + +my @ARGV = do { sub eval { &EVAL(@_) }; eval( ("0" and q|@*ARGS| or q|@ARGV|) ) }; + +my $numbers_tried = 0; +my $numbers_found = 0; + +NUMBERS: while ($numbers_found != @ARGV[0]) { + + $numbers_tried++; + my $state = $numbers_tried; + while ($state != 1) { + if ($state % 2 == 0) { + $state /= 2; + } + elsif ($state % 3 == 0) { + $state /= 3; + } + elsif ($state % 5 == 0) { + $state /= 5; + } + else { + next NUMBERS; + } + } + $numbers_found++; + + print("$numbers_tried\n"); +} diff --git a/challenge-003/nick-logan/perl6/ch-2.p6 b/challenge-003/nick-logan/perl6/ch-2.p6 new file mode 100644 index 0000000000..10bcd74535 --- /dev/null +++ b/challenge-003/nick-logan/perl6/ch-2.p6 @@ -0,0 +1,13 @@ +# WARNING: this polyglot breaks best practices of both Perl 5 and Perl 6 in order to run on both + +my @ARGV = do { sub eval { &EVAL(@_) }; eval( ("0" and q|@*ARGS| or q|@ARGV|) ) }; + +my @state = (1,); +for (1 .. @ARGV[0]) { + print(join(" ", @state), "\n"); + my @row = map &{ sub ($_) { @state[$_] + (@state[$_ + 1] // 0) } }.(), (0 .. ($_ - 2)); + @state = (); + push(@state, 1); + push(@state, $_) for @row; + push(@state, 1); +} diff --git a/challenge-003/ohmycloud/perl6/ch-1.p6 b/challenge-003/ohmycloud/perl6/ch-1.p6 new file mode 100644 index 0000000000..a29127d30c --- /dev/null +++ b/challenge-003/ohmycloud/perl6/ch-1.p6 @@ -0,0 +1,9 @@ +multi sub pascal (1) { $[1] } +multi sub pascal (Int $n where 2..*) { + my @rows = pascal $n - 1; + |@rows, [0, |@rows[*-1] Z+ |@rows[*-1], 0 ]; +} + +sub MAIN(Int $row) { + .say for pascal $row; +}
\ No newline at end of file diff --git a/challenge-003/ohmycloud/perl6/ch-2.p6 b/challenge-003/ohmycloud/perl6/ch-2.p6 new file mode 100644 index 0000000000..73fa14704f --- /dev/null +++ b/challenge-003/ohmycloud/perl6/ch-2.p6 @@ -0,0 +1,24 @@ + +sub ugly-number(Int $index) { + return 0 if $index == 0; + my @baselist = [1]; + my ($min2, $min3, $min5) = 0,0,0; + my $curnum = 1; + + while $curnum < $index { + my $minnum = (@baselist[$min2] * 2, @baselist[$min3] * 3, @baselist[$min5] * 5).min; + @baselist.append($minnum); + + while @baselist[$min2] * 2 <= $minnum { $min2 += 1 } + while @baselist[$min3] * 3 <= $minnum { $min3 += 1 } + while @baselist[$min5] * 5 <= $minnum { $min5 += 1 } + + $curnum +=1 + } + + return @baselist[*-1] +} + +sub MAIN(Int $count) { + ugly-number($_).say for 1..$count; +}
\ No newline at end of file diff --git a/challenge-003/rob4t/README b/challenge-003/rob4t/README new file mode 100644 index 0000000000..787d89fa0a --- /dev/null +++ b/challenge-003/rob4t/README @@ -0,0 +1 @@ +Solution by Robert Gratza diff --git a/challenge-003/rob4t/perl6/ch-1.p6 b/challenge-003/rob4t/perl6/ch-1.p6 new file mode 100644 index 0000000000..8e442553b0 --- /dev/null +++ b/challenge-003/rob4t/perl6/ch-1.p6 @@ -0,0 +1,22 @@ +#!/usr/bin/env perl6 +use v6; + +subset PositiveInt of Int where { $^n >= 0} + +sub MAIN(PositiveInt $amount = 5) { + say get-hamming-sequence()[0..$amount-1]; +} + +sub get-hamming-sequence() { + return lazy gather { take $_ if is-hamming-number($_) for 1..Inf }; +} + +sub is-hamming-number(PositiveInt $number is copy --> Bool) { + return True if $number == 1; + + for 2,3,5 -> $divisor { + return is-hamming-number($number div $divisor) if $number %% $divisor; + } + + return False; +}; diff --git a/challenge-003/rob4t/perl6/ch-2.p6 b/challenge-003/rob4t/perl6/ch-2.p6 new file mode 100644 index 0000000000..3d0b60786b --- /dev/null +++ b/challenge-003/rob4t/perl6/ch-2.p6 @@ -0,0 +1,27 @@ +#!/usr/bin/env perl6 +use v6; + +subset PositiveInt of Int where { $^n >= 0} +subset IntBigger3 of Int where { $^n >= 3} + +sub MAIN(IntBigger3 $rows) { + for 0..$rows-1 -> $i { + for 0 .. $i -> $j { + print " " if $j; + print get-binomial-coefficient($i, $j); + } + print "\n"; + } +} + +sub get-binomial-coefficient( PositiveInt $n is copy, PositiveInt $k is copy --> Int ) { + return 1 if $k == 0; + if (2*$k) > $n { + $k = $n - $k; + }; + my $r = 1; + for 1..$k { + $r = $r * ( $n - $k + $_ ) / $_; + } + return $r.Int; +}; diff --git a/challenge-003/sergiotarxz/perl5/ch-1.pl b/challenge-003/sergiotarxz/perl5/ch-1.pl new file mode 100644 index 0000000000..d4dedb814f --- /dev/null +++ b/challenge-003/sergiotarxz/perl5/ch-1.pl @@ -0,0 +1,9 @@ +use v5.28; +my @a = (2,3,5); +for (my $i=1; $i<$ARGV[0]; $i++) { + my $a = $i; + for (@a) { + $a/=$_ until $a%$_; + } + $a==1 and say $i; +} diff --git a/challenge-003/sergiotarxz/perl5/ch-2.pl b/challenge-003/sergiotarxz/perl5/ch-2.pl new file mode 100644 index 0000000000..e08c0b10d5 --- /dev/null +++ b/challenge-003/sergiotarxz/perl5/ch-2.pl @@ -0,0 +1,10 @@ +$ARGV[0] > 2 or die "This program requires at least 3 rows to work"; +for (my $i = 1; $i<=$ARGV[0]; $i++) { + my $c = 1; + print " "x($ARGV[0]-$i); + for (my $e = 1; $e<=$i; $e++) { + print "$c "; + $c = int($c*($i-$e)/$e); + } + print "\n"; +} diff --git a/challenge-003/simon-proctor/blog.txt b/challenge-003/simon-proctor/blog.txt new file mode 100644 index 0000000000..06d14d488b --- /dev/null +++ b/challenge-003/simon-proctor/blog.txt @@ -0,0 +1 @@ +http://www.khanate.co.uk/blog/2019/04/09/perl-weekly-week-3/ diff --git a/challenge-003/steve-rogerson/perl5/ch-1.pl b/challenge-003/steve-rogerson/perl5/ch-1.pl new file mode 100755 index 0000000000..e1378eaec3 --- /dev/null +++ b/challenge-003/steve-rogerson/perl5/ch-1.pl @@ -0,0 +1,29 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use List::Util 'min'; +use Const::Fast; +use 5.010; + + +sub hamming { + my ($arg) = @_; + $arg ||= 0; # pass 1 to restart. + state %s; + if (! %s or $arg ==1 ) { + %s = (1=>1); # 1 is the first hamming number. + } + my $next = min (keys %s); + delete $s{$next}; + for (2,3,5) { + $s{$next * $_} = 1; + } + return $next; +} + +my $i =0; +++$i, print hamming(), " " until $i > 20; +print "...\n"; + +++$i, hamming() until $i == 1690; +print ++$i, "-th: ", hamming(), "\n"; |
