diff options
| author | Dave Jacoby <jacoby.david@gmail.com> | 2022-02-21 16:57:27 -0500 |
|---|---|---|
| committer | Dave Jacoby <jacoby.david@gmail.com> | 2022-02-21 16:57:27 -0500 |
| commit | 22ebf18f9a9813ae1ed8d01181d53013d4ca8452 (patch) | |
| tree | 7fc71814b664f7502b7a7f353a6a54d0295a03e4 | |
| parent | 1b8a29378851e05454c2a4122a54cf3d36908899 (diff) | |
| download | perlweeklychallenge-club-22ebf18f9a9813ae1ed8d01181d53013d4ca8452.tar.gz perlweeklychallenge-club-22ebf18f9a9813ae1ed8d01181d53013d4ca8452.tar.bz2 perlweeklychallenge-club-22ebf18f9a9813ae1ed8d01181d53013d4ca8452.zip | |
153
| -rw-r--r-- | challenge-153/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-153/dave-jacoby/perl/ch-1.pl | 24 | ||||
| -rw-r--r-- | challenge-153/dave-jacoby/perl/ch-2.pl | 33 |
3 files changed, 58 insertions, 0 deletions
diff --git a/challenge-153/blog.txt b/challenge-153/blog.txt new file mode 100644 index 0000000000..05bb4357b4 --- /dev/null +++ b/challenge-153/blog.txt @@ -0,0 +1 @@ +https://jacoby.github.io/2022/02/21/luck-is-not-a-factor-weekly-challenge-153.html diff --git a/challenge-153/dave-jacoby/perl/ch-1.pl b/challenge-153/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..5a58b7f2b2 --- /dev/null +++ b/challenge-153/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,24 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature qw{ say postderef signatures state }; +no warnings qw{ experimental }; + +use List::Util qw{ sum0 product }; + +say join ', ', map { left_factorial($_) } 1 .. 10; + +# Left factorials: !n = Sum_{k=0..n-1} k!. +sub left_factorial( $n ) { + return sum0 map { factorial($_) } 0 .. $n - 1; +} + +sub factorial ( $n ) { + return 1 if $n == 0; + state $factorials ; + if ( !$factorials->{$n} ) { + $factorials->{$n} = product 1 .. $n; + } + return $factorials->{$n}; +} diff --git a/challenge-153/dave-jacoby/perl/ch-2.pl b/challenge-153/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..2cb74e5455 --- /dev/null +++ b/challenge-153/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,33 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature qw{ say postderef signatures state }; +no warnings qw{ experimental }; + +use List::Util qw{ sum0 product }; + +@ARGV = ( 123, 145 ) unless scalar @ARGV; + +for my $i (@ARGV) { + my $f = is_factorion($i); + say join "\t", '', $i, $f; +} + +sub is_factorion ( $n ) { + my $f = factorion($n); + return $f == $n ? 1 : 0; +} + +sub factorion ( $n ) { + return sum0 map { factorial($_) } split //, $n; +} + +sub factorial ( $n ) { + return 1 if $n == 0; + state $factorials ; + if ( !$factorials->{$n} ) { + $factorials->{$n} = product 1 .. $n; + } + return $factorials->{$n}; +} |
