Last active
April 14, 2019 23:06
-
-
Save adamcrussell/268edfef6fd85ec2d61daa84f4ae5695 to your computer and use it in GitHub Desktop.
Perl Weekly Challenge 003
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment