diff options
| -rwxr-xr-x | challenge-153/mattneleigh/perl/ch-1.pl | 64 | ||||
| -rwxr-xr-x | challenge-153/mattneleigh/perl/ch-2.pl | 75 |
2 files changed, 139 insertions, 0 deletions
diff --git a/challenge-153/mattneleigh/perl/ch-1.pl b/challenge-153/mattneleigh/perl/ch-1.pl new file mode 100755 index 0000000000..f2b3f20e91 --- /dev/null +++ b/challenge-153/mattneleigh/perl/ch-1.pl @@ -0,0 +1,64 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my $n = 10; + +printf( + "\nThe first %d Left Factorials are:\n %s\n\n", + $n, + join(", ", n_left_factorials($n)) +); + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Calculate the first N Left Factorials (See OEIS A003422) +# Takes one argument: +# * A positive integer indicating how many Left Factorials to calculate +# Returns on success: +# * A list of the first N Left Factorials +# Returns on error: +# * undef if N is not a positive integer +################################################################################ +sub n_left_factorials{ + my $n = int(shift()); + + return(undef) + unless($n > 0); + + my $i; + my $factorial; + my @left_factorials; + + # Initial setup + push(@left_factorials, 1); + $factorial = 1; + + for $i (1 .. ($n - 1)){ + # Multiply our running product by $i, then + # store its sum with the previous sum + $factorial *= $i; + push( + @left_factorials, + $left_factorials[$#left_factorials] + $factorial + ); + } + + return(@left_factorials); + +} + + + diff --git a/challenge-153/mattneleigh/perl/ch-2.pl b/challenge-153/mattneleigh/perl/ch-2.pl new file mode 100755 index 0000000000..9e71c1f72f --- /dev/null +++ b/challenge-153/mattneleigh/perl/ch-2.pl @@ -0,0 +1,75 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +# Need this for state variables +use v5.10; + +################################################################################ +# Begin main execution +################################################################################ + +my @numbers = ( + 145, + 125 +); +my $number; + +foreach $number (@numbers){ + printf("\nInput: \$n = %d\nOutput: %d\n\n", + $number, + is_factorion($number) + ); +} + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Determine whether a number N is a Factorion (it is equal to the sum of its +# digits' factorials) +# Takes one argument: +# * The number N to examine +# Returns on success: +# * 1 if N is a Factorion +# * 0 if N is not a Factorion +# Returns on error: +# * undef if N is less than zero +################################################################################ +sub is_factorion{ + my $n = int(shift()); + + return(undef) + if($n < 0); + + state @factorials; + my $sum = 0; + + unless(@factorials){ + # Factorial list wasn't defined... generate + # it; this only happens the first time this + # function is called + push(@factorials, 1); + for(1 .. 9){ + push(@factorials, $factorials[$#factorials] * $ARG); + } + } + + # Split the number into digits and add up + # the factorials of each digit + foreach(split('', $n)){ + $sum += $factorials[$ARG]; + } + + return($sum == $n); + +} + + + |
