diff options
Diffstat (limited to 'challenge-153')
| -rwxr-xr-x | challenge-153/pete-houston/perl/ch-1.pl | 30 | ||||
| -rwxr-xr-x | challenge-153/pete-houston/perl/ch-2.pl | 47 |
2 files changed, 77 insertions, 0 deletions
diff --git a/challenge-153/pete-houston/perl/ch-1.pl b/challenge-153/pete-houston/perl/ch-1.pl new file mode 100755 index 0000000000..f668a2919e --- /dev/null +++ b/challenge-153/pete-houston/perl/ch-1.pl @@ -0,0 +1,30 @@ +#!/usr/bin/env perl +#=============================================================================== +# +# FILE: 15301.pl +# +# USAGE: ./15301.pl +# +# DESCRIPTION: Output left factorials of 1 to 10 +# +# REQUIREMENTS: Perl 5.10.0 for defined-or and say +# NOTES: See http://oeis.org/A003422 for left factorials +# AUTHOR: Pete Houston (pete), cpan@openstrike.co.uk +# ORGANIZATION: Openstrike +# VERSION: 1.0 +# CREATED: 21/02/22 +#=============================================================================== + +use strict; +use warnings; +use 5.010; + +my @fac = 1; # Factorials (0! = 1 by definition) +my @lf; # Left Factorials + +for my $i (1 .. 10) { + push @fac, $fac[$i-1] * $i; + push @lf, ($lf[-1] // 0) + $fac[$i-1]; +} + +say join ', ', @lf; diff --git a/challenge-153/pete-houston/perl/ch-2.pl b/challenge-153/pete-houston/perl/ch-2.pl new file mode 100755 index 0000000000..2255e37d3b --- /dev/null +++ b/challenge-153/pete-houston/perl/ch-2.pl @@ -0,0 +1,47 @@ +#!/usr/bin/env perl +#=============================================================================== +# +# FILE: 15302.pl +# +# USAGE: ./15302.pl N +# +# DESCRIPTION: Is the provided number a factorion? +# +# REQUIREMENTS: Perl 5.10.0 for 'state' +# NOTES: This could easily be written with List::Util::sum and +# Math::Prime::Util::factorial but where's the challenge +# in that? +# AUTHOR: Pete Houston (pete), cpan@openstrike.co.uk +# ORGANIZATION: Openstrike +# VERSION: 1.0 +# CREATED: 21/02/22 +#=============================================================================== + +use strict; +use warnings; +use feature 'state'; + +printf "%i\n", is_factorion ($ARGV[0]) ? 1 : 0; + +# Instead of List::Util::sum +sub sum (@) { + my $tot = shift; + $tot += $_ for @_; + return $tot; +} + +# Instead of Math::Prime::Util::factorial +sub fac { + state $have = { 0 => 1 }; + my $i = shift; + return $have->{$i} if $have->{$i}; + my $fac = $i * fac ($i - 1); + $have->{$i} = $fac; + return $fac; +} + +sub is_factorion { + my $n = shift; + return 0 unless defined ($n) && $n =~ /^[1-9][0-9]*$/; + return $n == sum map { fac ($_) } split //, $n; +} |
