diff options
| -rw-r--r-- | challenge-003/adam-russell/blog.txt | 2 | ||||
| -rw-r--r-- | challenge-003/adam-russell/perl5/ch-1.pl | 47 | ||||
| -rw-r--r-- | challenge-003/adam-russell/perl5/ch-2.pl | 43 |
3 files changed, 90 insertions, 2 deletions
diff --git a/challenge-003/adam-russell/blog.txt b/challenge-003/adam-russell/blog.txt index 8be9455506..986b3b3b95 100644 --- a/challenge-003/adam-russell/blog.txt +++ b/challenge-003/adam-russell/blog.txt @@ -1 +1 @@ -XXXhttps://adamcrussell.livejournal.com/620.html +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 index 199e274ec1..06a3d038d3 100644 --- a/challenge-003/adam-russell/perl5/ch-1.pl +++ b/challenge-003/adam-russell/perl5/ch-1.pl @@ -4,4 +4,49 @@ 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 index b1288d4870..fd8242e74f 100644 --- a/challenge-003/adam-russell/perl5/ch-2.pl +++ b/challenge-003/adam-russell/perl5/ch-2.pl @@ -6,3 +6,46 @@ use warnings; # 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"; +} |
