diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2019-04-14 20:56:04 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-04-14 20:56:04 +0100 |
| commit | 71cb4e9c559a39873423be86335e1f4a6ff1fd61 (patch) | |
| tree | 83013f63571bc88c4d510eb4fa2538d564ca509a | |
| parent | afbe70d6d43d71e51db8a034ca9ed6ab0d44bc53 (diff) | |
| parent | cd5271b1e65d4e56f1988f5832cebdea90778da0 (diff) | |
| download | perlweeklychallenge-club-71cb4e9c559a39873423be86335e1f4a6ff1fd61.tar.gz perlweeklychallenge-club-71cb4e9c559a39873423be86335e1f4a6ff1fd61.tar.bz2 perlweeklychallenge-club-71cb4e9c559a39873423be86335e1f4a6ff1fd61.zip | |
Merge pull request #53 from jmaslak/jmaslak-week3
Jmaslak week3
| -rwxr-xr-x | challenge-003/joelle-maslak/perl5/ch-1.pl | 80 | ||||
| -rwxr-xr-x | challenge-003/joelle-maslak/perl5/ch-2.pl | 35 | ||||
| -rwxr-xr-x[-rw-r--r--] | challenge-003/joelle-maslak/perl6/ch-1.p6 | 6 | ||||
| -rwxr-xr-x[-rw-r--r--] | challenge-003/joelle-maslak/perl6/ch-2.p6 | 2 |
4 files changed, 120 insertions, 3 deletions
diff --git a/challenge-003/joelle-maslak/perl5/ch-1.pl b/challenge-003/joelle-maslak/perl5/ch-1.pl new file mode 100755 index 0000000000..da088b4a6e --- /dev/null +++ b/challenge-003/joelle-maslak/perl5/ch-1.pl @@ -0,0 +1,80 @@ +#!/usr/bin/env perl + +use v5.26; +use strict; +use warnings; + +# Turn on method signatures +use feature 'signatures'; +no warnings 'experimental::signatures'; + +if ( !@ARGV ) { die("Provide number of hamming numbers to return") } +if ( $ARGV[0] < 0 ) { die("Must provide a positive number") } +if ( int( $ARGV[0] ) != $ARGV[0] ) { die("Must provide an integer") } + +my $count = $ARGV[0]; + +my @hamming; +while ( scalar(@hamming) < $count ) { + state $i = 1; + + push @hamming, $i if ( ishamming($i) ); + + $i++; +} + +say "Hamming numbers [0.." . ( $count - 1 ) . "]: " . join( ", ", @hamming ); + +sub ishamming($i) { + my @primes = primedivisors($i); + my @smallprimes = grep { $_ <= 5 } @primes; + + if ( @primes == @smallprimes ) { + return 1; + } else { + return; + } +} + +sub primedivisors($i) { + my %primes; + + my @divisors = divisors($i); + if ( scalar(@divisors) <= 2 ) { $primes{$i} = 1 } + + while ( my $div = shift @divisors ) { + if ( $div == 1 ) { next; } + if ( $div == $i ) { next; } + + my @divdivs = divisors($div); + if ( @divdivs <= 2 ) { # Prime + $primes{$div} = 1; + } else { + my @newdivs = primedivisors($div); + foreach my $prime (@newdivs) { + $primes{$prime} = 1; + } + } + } + + return sort { $a <=> $b } keys %primes; +} + +sub divisors($i) { + state $cache = { 0 => [] }; + if ( ref($i) eq 'ARRAY' ) { confess("WHAT?") } + + if ( $i < 0 ) { die("Must provide a postiive integer") } + if ( int($i) != $i ) { die("Must provide a positive integer") } + + if ( defined( $cache->{$i} ) ) { return @{ $cache->{$i} } } + + my $sqrt = int( sqrt($i) ); + + my @divs = grep { !( $i % $_ ) } 1 .. $sqrt; + push @divs, map { int( $i / $_ ) } @divs; + + $cache->{$i} = \@divs; + return @divs; +} + diff --git a/challenge-003/joelle-maslak/perl5/ch-2.pl b/challenge-003/joelle-maslak/perl5/ch-2.pl new file mode 100755 index 0000000000..bdc3cd8c51 --- /dev/null +++ b/challenge-003/joelle-maslak/perl5/ch-2.pl @@ -0,0 +1,35 @@ +#!/usr/bin/env perl + +use v5.26; +use strict; +use warnings; + +# Turn on method signatures +use feature 'signatures'; +no warnings 'experimental::signatures'; + +if ( !@ARGV ) { die("Provide number of rows") } +if ( $ARGV[0] < 3 ) { die("Must provide at least 3 rows") } +if ( int( $ARGV[0] ) != $ARGV[0] ) { die("Must provide an integer") } + +my $rowcount = $ARGV[0]; + +my @rows; + +for ( my $i = 0; $i < $rowcount; $i++ ) { + my @row; + if ( !$i ) { + @row = 1; + } else { + push @row, 1; + for ( my $j = 1; $j < $i; $j++ ) { + push @row, $rows[ $i - 1 ]->[ $j - 1 ] + $rows[ $i - 1 ]->[$j]; + } + push @row, 1; + } + push @rows, \@row; + + for ( my $j = 1; $j < $rowcount - $i; $j++ ) { print(" ") } + say join( " ", map { sprintf( "%3d", $_ ) } @{ $rows[$i] } ); +} + diff --git a/challenge-003/joelle-maslak/perl6/ch-1.p6 b/challenge-003/joelle-maslak/perl6/ch-1.p6 index d9649489c7..f93e7683ab 100644..100755 --- a/challenge-003/joelle-maslak/perl6/ch-1.p6 +++ b/challenge-003/joelle-maslak/perl6/ch-1.p6 @@ -2,8 +2,10 @@ 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 MAIN(Int:D $count where * ≥ 0) { + my @hamming = (1..∞).grep: { divisors($^num).grep( *.is-prime ).Set ⊆ (2,3,5).Set }; + say "Hamming numbers [0..{$count-1}]: " ~ @hamming[^$count].join(", "); +} sub divisors(Int:D $i -->Array[Int:D]) { if ($i == 0) { return 0; } diff --git a/challenge-003/joelle-maslak/perl6/ch-2.p6 b/challenge-003/joelle-maslak/perl6/ch-2.p6 index 3200dfc2bf..e45424fc44 100644..100755 --- a/challenge-003/joelle-maslak/perl6/ch-2.p6 +++ b/challenge-003/joelle-maslak/perl6/ch-2.p6 @@ -16,7 +16,7 @@ sub MAIN(Int:D $rows where $rows ≥ 3) { } @rows.push: @row; - say (" " x $rows - $i) ~ @row.map( { $^a.fmt("%3d") } ).join(" "); + say (" " x $rows - $i - 1) ~ @row.map( { $^a.fmt("%3d") } ).join(" "); } } |
