Skip to content

Instantly share code, notes, and snippets.

@adamcrussell
Created September 1, 2019 21:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamcrussell/366110b35d8edbc63040b723d673138f to your computer and use it in GitHub Desktop.
Save adamcrussell/366110b35d8edbc63040b723d673138f to your computer and use it in GitHub Desktop.
Perl Weekly Challenge 023
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";
}
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