aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-02-23 08:03:38 +0000
committerGitHub <noreply@github.com>2022-02-23 08:03:38 +0000
commit14f1f3cdad4cb7c2b5610ae1cb5342bdf7fa1fb5 (patch)
tree634391852528057019013fb7dee187a6264125e7
parent7746905c2b0130b08be4d8fb60cf6e636ed04b34 (diff)
parentf683af188a4929a8d947d7a213178483eb8f07ed (diff)
downloadperlweeklychallenge-club-14f1f3cdad4cb7c2b5610ae1cb5342bdf7fa1fb5.tar.gz
perlweeklychallenge-club-14f1f3cdad4cb7c2b5610ae1cb5342bdf7fa1fb5.tar.bz2
perlweeklychallenge-club-14f1f3cdad4cb7c2b5610ae1cb5342bdf7fa1fb5.zip
Merge pull request #5698 from mattneleigh/pwc153
new file: challenge-153/mattneleigh/perl/ch-1.pl
-rwxr-xr-xchallenge-153/mattneleigh/perl/ch-1.pl64
-rwxr-xr-xchallenge-153/mattneleigh/perl/ch-2.pl75
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);
+
+}
+
+
+