aboutsummaryrefslogtreecommitdiff
path: root/challenge-153
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-02-22 08:56:40 +0000
committerGitHub <noreply@github.com>2022-02-22 08:56:40 +0000
commitf39e0f071fa13dbc06216f5d0c5cc762124b9717 (patch)
tree41ff7b3476ebe37cf7086f408992bd827a188cf0 /challenge-153
parent4ee26847441a3daaee9704c8d0dc5c7a26317de5 (diff)
parent22ebf18f9a9813ae1ed8d01181d53013d4ca8452 (diff)
downloadperlweeklychallenge-club-f39e0f071fa13dbc06216f5d0c5cc762124b9717.tar.gz
perlweeklychallenge-club-f39e0f071fa13dbc06216f5d0c5cc762124b9717.tar.bz2
perlweeklychallenge-club-f39e0f071fa13dbc06216f5d0c5cc762124b9717.zip
Merge pull request #5695 from jacoby/master
Challenge #153
Diffstat (limited to 'challenge-153')
-rw-r--r--challenge-153/blog.txt1
-rw-r--r--challenge-153/dave-jacoby/perl/ch-1.pl24
-rw-r--r--challenge-153/dave-jacoby/perl/ch-2.pl33
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};
+}