Created
September 1, 2019 21:18
-
-
Save adamcrussell/366110b35d8edbc63040b723d673138f to your computer and use it in GitHub Desktop.
Perl Weekly Challenge 023
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 prints nth order forward difference series. | |
## | |
sub forward_difference{ | |
my($order, $numbers) = @_; | |
if(!$order || @{$numbers} == 1){ | |
return $numbers; | |
} | |
else{ | |
my $reduced = []; | |
my $previous = $numbers->[0]; | |
for(my $i = 1; $i < @{$numbers}; $i++){ | |
push @{$reduced}, $numbers->[$i] - $previous; | |
$previous = $numbers->[$i]; | |
} | |
forward_difference($order - 1, $reduced); | |
} | |
} | |
MAIN:{ | |
my $order = $ARGV[0]; | |
my @numbers = @ARGV[1 .. (@ARGV - 1)]; | |
my $reduced = forward_difference($order, \@numbers); | |
print join(", ", @{$reduced}) . "\n"; | |
} |
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 prints Prime Decomposition of a given number. | |
## | |
sub prime_factor{ | |
my $x = shift(@_); | |
my @factors; | |
for (my $y = 2; $y <= $x; $y++){ | |
next if $x % $y; | |
$x /= $y; | |
push @factors, $y; | |
redo; | |
} | |
return @factors; | |
} | |
MAIN:{ | |
my $n = $ARGV[0]; | |
print join(", ", prime_factor($n)) . "\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment