aboutsummaryrefslogtreecommitdiff
path: root/challenge-003
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2019-04-14 22:48:26 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2019-04-14 22:48:26 +0100
commit48e898f0ff0ab4462405fc0cc2895330db69a4e3 (patch)
treef44619603558a6a9ee78b3eb1550f5a1802f513e /challenge-003
parent646dff1b90f910c0e3939e3c0d05b220492bae18 (diff)
parent71cb4e9c559a39873423be86335e1f4a6ff1fd61 (diff)
downloadperlweeklychallenge-club-48e898f0ff0ab4462405fc0cc2895330db69a4e3.tar.gz
perlweeklychallenge-club-48e898f0ff0ab4462405fc0cc2895330db69a4e3.tar.bz2
perlweeklychallenge-club-48e898f0ff0ab4462405fc0cc2895330db69a4e3.zip
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
Diffstat (limited to 'challenge-003')
-rwxr-xr-xchallenge-003/joelle-maslak/perl5/ch-1.pl80
-rwxr-xr-xchallenge-003/joelle-maslak/perl5/ch-2.pl35
-rwxr-xr-x[-rw-r--r--]challenge-003/joelle-maslak/perl6/ch-1.p66
-rwxr-xr-x[-rw-r--r--]challenge-003/joelle-maslak/perl6/ch-2.p62
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(" ");
}
}