aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2019-04-14 16:42:32 +0100
committerGitHub <noreply@github.com>2019-04-14 16:42:32 +0100
commit4d2c45a9c0193695b00e792dbb981dccf12afe16 (patch)
tree7b899acfc23dc37c000ad1391afe43956d871d5d
parent4abe3535afff1e5632e9a7755b899c2ed39b1ec5 (diff)
parentf0059bab33893a7f86ca5eae99ae149e597c816e (diff)
downloadperlweeklychallenge-club-4d2c45a9c0193695b00e792dbb981dccf12afe16.tar.gz
perlweeklychallenge-club-4d2c45a9c0193695b00e792dbb981dccf12afe16.tar.bz2
perlweeklychallenge-club-4d2c45a9c0193695b00e792dbb981dccf12afe16.zip
Merge pull request #51 from adamcrussell/master
Adam Russell solution for challenge 003
-rw-r--r--challenge-003/adam-russell/blog.txt1
-rw-r--r--challenge-003/adam-russell/perl5/ch-1.pl52
-rw-r--r--challenge-003/adam-russell/perl5/ch-2.pl51
3 files changed, 104 insertions, 0 deletions
diff --git a/challenge-003/adam-russell/blog.txt b/challenge-003/adam-russell/blog.txt
new file mode 100644
index 0000000000..986b3b3b95
--- /dev/null
+++ b/challenge-003/adam-russell/blog.txt
@@ -0,0 +1 @@
+https://adamcrussell.livejournal.com/948.html
diff --git a/challenge-003/adam-russell/perl5/ch-1.pl b/challenge-003/adam-russell/perl5/ch-1.pl
new file mode 100644
index 0000000000..06a3d038d3
--- /dev/null
+++ b/challenge-003/adam-russell/perl5/ch-1.pl
@@ -0,0 +1,52 @@
+use strict;
+use warnings;
+##
+# 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.
+##
+use boolean;
+
+sub is_divisible{
+ my($divisor) = @_;
+ return sub{
+ my($x) = @_;
+ if($x % $divisor == 0){
+ return true;
+ }
+ return false;
+ }
+}
+
+my $is_divisible_by_2 = is_divisible(2);
+my $is_divisible_by_3 = is_divisible(3);
+my $is_divisible_by_5 = is_divisible(5);
+
+sub is_hamming{
+ my($x) = @_;
+ if($x == 1){
+ return true;
+ }
+ if($is_divisible_by_2->($x)){
+ return is_hamming($x/2);
+ }
+ if($is_divisible_by_3->($x)){
+ return is_hamming($x/3);
+ }
+ if($is_divisible_by_5->($x)){
+ return is_hamming($x/5);
+ }
+ return false;
+}
+
+sub generate_hamming_sequence{
+ my($x) = @_;
+ if($x == 1){
+ return true;
+ }
+ generate_hamming_sequence($x - 1);
+ if(is_hamming($x)){
+ print("$x ");
+ }
+}
+
+generate_hamming_sequence(64);
diff --git a/challenge-003/adam-russell/perl5/ch-2.pl b/challenge-003/adam-russell/perl5/ch-2.pl
new file mode 100644
index 0000000000..fd8242e74f
--- /dev/null
+++ b/challenge-003/adam-russell/perl5/ch-2.pl
@@ -0,0 +1,51 @@
+use strict;
+use warnings;
+##
+# Create a script that generates Pascal Triangle.
+# Accept number of rows from the command line.
+# The Pascal Triangle should have at least 3 rows.
+##
+
+##
+# How to compute Pascal's Triangle.
+# After the inital row(s) with 1 entries, the entry for the n-th row and k-th column is defined as
+# (n-1)! (n-1)!
+# -------------------- + ------------
+# (k-1)!((n-1)-(k-1))! k!((n-1)-k)!
+##
+
+sub compute_entry{
+ my($row, $column) = @_;
+ unless($row < 2){
+ my($x0, $y0, $x1, $y1);
+ $x0 = factorial($row - 1);
+ $y0 = factorial($column - 1) * factorial(($row - 1) - ($column - 1));
+ $x1 = factorial($row - 1);
+ $y1 = factorial($column) * factorial(($row - 1) - $column);
+ return int($x0 / $y0) + int($x1 / $y1);
+ }
+ return 1;
+}
+
+sub factorial{
+ my($n) = @_;
+ unless($n < 1){
+ return $n * factorial($n - 1);
+ }
+ return 1;
+}
+
+##
+# Main
+##
+my $max = $ARGV[0];
+my $padding = " ";
+my $padding_length = $max;
+for my $i (1 .. $max){
+ $padding_length = $padding_length - 1;
+ print $padding x $padding_length;
+ for my $j (1 .. $i){
+ print compute_entry($i - 1, $j - 1) . " ";
+ }
+ print "\n";
+}