From dcd5dc8d7f0e8cec0e329f8abadbdfcc583fdfa3 Mon Sep 17 00:00:00 2001 From: Joelle Maslak Date: Sun, 14 Apr 2019 14:34:36 -0500 Subject: Allow specifying number count on command line --- challenge-003/joelle-maslak/perl6/ch-1.p6 | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) mode change 100644 => 100755 challenge-003/joelle-maslak/perl6/ch-1.p6 diff --git a/challenge-003/joelle-maslak/perl6/ch-1.p6 b/challenge-003/joelle-maslak/perl6/ch-1.p6 old mode 100644 new mode 100755 index d9649489c7..f93e7683ab --- 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; } -- cgit From 5eac99ea07fc806d01749594cffaddaacdc05593 Mon Sep 17 00:00:00 2001 From: Joelle Maslak Date: Sun, 14 Apr 2019 14:34:52 -0500 Subject: Make executable --- challenge-003/joelle-maslak/perl6/ch-2.p6 | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 challenge-003/joelle-maslak/perl6/ch-2.p6 diff --git a/challenge-003/joelle-maslak/perl6/ch-2.p6 b/challenge-003/joelle-maslak/perl6/ch-2.p6 old mode 100644 new mode 100755 -- cgit From d0ae7967da53c34f3715f54ef62951eb07b42dad Mon Sep 17 00:00:00 2001 From: Joelle Maslak Date: Sun, 14 Apr 2019 14:35:15 -0500 Subject: Add Perl 5 week 3, problem 1 solution --- challenge-003/joelle-maslak/perl5/ch-1.pl | 80 +++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 challenge-003/joelle-maslak/perl5/ch-1.pl diff --git a/challenge-003/joelle-maslak/perl5/ch-1.pl b/challenge-003/joelle-maslak/perl5/ch-1.pl new file mode 100644 index 0000000000..45642bd764 --- /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; +} + -- cgit From b600c7a2226089c8f347f91769c63f9d9460cc68 Mon Sep 17 00:00:00 2001 From: Joelle Maslak Date: Sun, 14 Apr 2019 14:47:09 -0500 Subject: Get rid of extra spaces --- challenge-003/joelle-maslak/perl6/ch-2.p6 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-003/joelle-maslak/perl6/ch-2.p6 b/challenge-003/joelle-maslak/perl6/ch-2.p6 index 3200dfc2bf..e45424fc44 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(" "); } } -- cgit From 8bfd4256fe81493bf8c0e04c0feb89122ec1bfc4 Mon Sep 17 00:00:00 2001 From: Joelle Maslak Date: Sun, 14 Apr 2019 14:47:28 -0500 Subject: Minor formatting change --- challenge-003/joelle-maslak/perl5/ch-1.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) mode change 100644 => 100755 challenge-003/joelle-maslak/perl5/ch-1.pl diff --git a/challenge-003/joelle-maslak/perl5/ch-1.pl b/challenge-003/joelle-maslak/perl5/ch-1.pl old mode 100644 new mode 100755 index 45642bd764..da088b4a6e --- a/challenge-003/joelle-maslak/perl5/ch-1.pl +++ b/challenge-003/joelle-maslak/perl5/ch-1.pl @@ -23,7 +23,7 @@ while ( scalar(@hamming) < $count ) { $i++; } -say "Hamming numbers [0.." . ($count-1) . "]: " . join( ", ", @hamming ); +say "Hamming numbers [0.." . ( $count - 1 ) . "]: " . join( ", ", @hamming ); sub ishamming($i) { my @primes = primedivisors($i); -- cgit From cd5271b1e65d4e56f1988f5832cebdea90778da0 Mon Sep 17 00:00:00 2001 From: Joelle Maslak Date: Sun, 14 Apr 2019 14:47:48 -0500 Subject: Add solution for week 3, problem 2 in Perl 5 --- challenge-003/joelle-maslak/perl5/ch-2.pl | 35 +++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100755 challenge-003/joelle-maslak/perl5/ch-2.pl 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] } ); +} + -- cgit