diff options
| -rw-r--r-- | challenge-003/adam-russell/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-003/adam-russell/perl5/ch-1.pl | 52 | ||||
| -rw-r--r-- | challenge-003/adam-russell/perl5/ch-2.pl | 51 |
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"; +} |
