aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-11-15 01:55:50 +0000
committerGitHub <noreply@github.com>2021-11-15 01:55:50 +0000
commit2a9aec2774701a978bfb6a93b13b2525e3f96f15 (patch)
tree3e7e6dd835a82102ef23d430eb42feba92759f1c
parentcc2fe03b83e17a11590d6681bf8e5547e5ea5a9a (diff)
parent17fd72285fae20e3f79d53225cf5336394c4f69b (diff)
downloadperlweeklychallenge-club-2a9aec2774701a978bfb6a93b13b2525e3f96f15.tar.gz
perlweeklychallenge-club-2a9aec2774701a978bfb6a93b13b2525e3f96f15.tar.bz2
perlweeklychallenge-club-2a9aec2774701a978bfb6a93b13b2525e3f96f15.zip
Merge pull request #5221 from boblied/w003
W003
-rw-r--r--challenge-003/bob-lied/perl/ch-1.pl62
-rw-r--r--challenge-003/bob-lied/perl/ch-2.pl48
2 files changed, 110 insertions, 0 deletions
diff --git a/challenge-003/bob-lied/perl/ch-1.pl b/challenge-003/bob-lied/perl/ch-1.pl
new file mode 100644
index 0000000000..024fbed8f1
--- /dev/null
+++ b/challenge-003/bob-lied/perl/ch-1.pl
@@ -0,0 +1,62 @@
+#!/usr/bin/env perl
+# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu:
+#=============================================================================
+# ch-1.pl
+#=============================================================================
+# Copyright (c) 2021, Bob Lied
+#=============================================================================
+# Perl Weekly challenge Week 3, 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.
+# "... numbers are called 5-smooth, because they can be characterized as
+# having only 2, 3, or 5 as prime factors. ..."
+#=============================================================================
+
+use strict;
+use warnings;
+use v5.32;
+
+use experimental qw/ signatures /;
+no warnings "experimental::signatures";
+
+use Getopt::Long;
+my $Verbose = 0;
+my $DoTest = 0;
+
+GetOptions("test" => \$DoTest, "verbose" => \$Verbose);
+my $Max = shift;
+$Max //= 100;
+
+sub fiveSmooth($max)
+{
+ my @smoothNumbers;
+ for ( my $mult2 = 1; $mult2 <= $max ; $mult2 *= 2 )
+ {
+ for ( my $mult3 = $mult2 ; $mult3 <= $max ; $mult3 *= 3 )
+ {
+ for ( my $mult5 = $mult3 ; $mult5 <= $max ; $mult5 *= 5 )
+ {
+ push @smoothNumbers, $mult5 unless $mult5 == 1; # Not in order
+ }
+ }
+ }
+ return \@smoothNumbers;
+}
+exit(!runTest()) if $DoTest;
+my $smoothList = fiveSmooth($Max);
+say $_ foreach sort { $a <=> $b} @$smoothList;
+
+sub runTest
+{
+ use Test::More;
+
+ is_deeply( fiveSmooth( 2), [ 2 ], "max = 2");
+ is_deeply( fiveSmooth( 3), [ 2, 3 ], "max = 3");
+ is_deeply( fiveSmooth( 5), [ 2, 3, 4, 5 ], "max = 5");
+ is_deeply( fiveSmooth(10), [ 2, 3, 4, 5, 6, 8, 9, 10 ], "max = 10");
+ is_deeply( fiveSmooth(20), [ 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20 ], "max = 20");
+
+ done_testing;
+}
+
diff --git a/challenge-003/bob-lied/perl/ch-2.pl b/challenge-003/bob-lied/perl/ch-2.pl
new file mode 100644
index 0000000000..c30ed80b37
--- /dev/null
+++ b/challenge-003/bob-lied/perl/ch-2.pl
@@ -0,0 +1,48 @@
+#!/usr/bin/env perl
+# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu:
+#=============================================================================
+# ch-2.pl
+#=============================================================================
+# Copyright (c) 2021, Bob Lied
+#=============================================================================
+# Perl Weekly Challenge Week 3, 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.
+#=============================================================================
+
+use strict;
+use warnings;
+use v5.32;
+
+use experimental qw/ signatures /;
+no warnings "experimental::signatures";
+
+use Getopt::Long;
+my $Verbose = 0;
+my $DoTest = 0;
+
+my $NumRows = shift;
+die "Usage: $0 N, where N >= 1" unless $NumRows > 0;
+
+sub pascalTriangle($n)
+{
+ say "1";
+ my $prevRow = [ 1, 1 ];
+ say "@$prevRow" if $n >= 2;
+
+ while ( --$n > 1 )
+ {
+ my $nextRow = [ 1 ];
+ for ( my $c = 0 ; $c < scalar(@$prevRow)-1; $c++ )
+ {
+ push @$nextRow, $prevRow->[$c] + $prevRow->[$c+1];
+ }
+ push @$nextRow, 1;
+ say "@$nextRow";
+ $prevRow = $nextRow;
+ }
+
+}
+
+pascalTriangle($NumRows);