aboutsummaryrefslogtreecommitdiff
path: root/challenge-153
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2022-02-25 17:37:09 +0000
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2022-02-25 17:37:09 +0000
commit2a29f468d04d04891e6a637bbc6c0e8e25ea1527 (patch)
tree2daab0beea4dcd005baec3ed1bac12a02ceab864 /challenge-153
parent885f847ec3972ff6cd672410cb384e7846affbbf (diff)
parente8d69b06889134c9d487a13c9b3afb67d54d4fcc (diff)
downloadperlweeklychallenge-club-2a29f468d04d04891e6a637bbc6c0e8e25ea1527.tar.gz
perlweeklychallenge-club-2a29f468d04d04891e6a637bbc6c0e8e25ea1527.tar.bz2
perlweeklychallenge-club-2a29f468d04d04891e6a637bbc6c0e8e25ea1527.zip
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
Diffstat (limited to 'challenge-153')
-rwxr-xr-xchallenge-153/jo-37/perl/ch-1.pl69
-rwxr-xr-xchallenge-153/jo-37/perl/ch-2.pl66
2 files changed, 135 insertions, 0 deletions
diff --git a/challenge-153/jo-37/perl/ch-1.pl b/challenge-153/jo-37/perl/ch-1.pl
new file mode 100755
index 0000000000..861e8d0a13
--- /dev/null
+++ b/challenge-153/jo-37/perl/ch-1.pl
@@ -0,0 +1,69 @@
+#!/usr/bin/perl -s
+
+use v5.16;
+use Test2::V0;
+use bigint;
+use Coro::Generator;
+
+our ($tests, $examples);
+
+run_tests() if $tests || $examples; # does not return
+
+die <<EOS unless @ARGV;
+usage: $0 [-examples] [-tests] [N]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+N
+ Print the first N left factorials.
+
+EOS
+
+
+### Input and Output
+
+main: {
+ my $left_factorial = gen_left_factorial();
+ say $left_factorial->() for 1 .. $ARGV[0];
+}
+
+
+### Implementation
+
+# Build a generator for left factorials
+sub gen_left_factorial {
+ my ($index, $factorial, $left_factorial) = (0, 1, 1);
+
+ generator {
+ yield $left_factorial;
+ yield $left_factorial += ($factorial *= ++$index) while 1;
+ }
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ my $left_factorial = gen_left_factorial();
+ is [map $left_factorial->(), 1 .. 10],
+ [1, 2, 4, 10, 34, 154, 874, 5914, 46234, 409114], 'task 1';
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+
+ my $left_factorial = gen_left_factorial();
+ $left_factorial->() for 1 .. 21;
+ is $left_factorial->(), 53652269665821260314, 'from OEIS';
+ }
+
+ done_testing;
+ exit;
+}
diff --git a/challenge-153/jo-37/perl/ch-2.pl b/challenge-153/jo-37/perl/ch-2.pl
new file mode 100755
index 0000000000..06d76ad7b2
--- /dev/null
+++ b/challenge-153/jo-37/perl/ch-2.pl
@@ -0,0 +1,66 @@
+#!/usr/bin/perl -s
+
+use v5.16;
+use Test2::V0;
+use Math::Prime::Util qw(factorial vecsum todigits vecnone);
+use experimental 'signatures';
+
+our ($tests, $examples, $verbose, $base);
+
+$base ||= 10;
+
+run_tests() if $tests || $examples; # does not return
+
+die <<EOS unless @ARGV;
+usage: $0 [-examples] [-tests] [-verbose] [N]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+-base=B
+ Check factorions in base B. Default: 10
+
+N
+ Check if N is a factorion in base B
+
+EOS
+
+
+### Input and Output
+
+say 0 + factorion($ARGV[0], $base);
+
+
+### Implementation
+
+sub factorion($n, $base=10) {
+ $n == vecsum map factorial($_), todigits $n, $base;
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ ok factorion(145), 'example 1';
+ ok !factorion(125), 'example 2';
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+
+ ok factorion(40585), 'one more from WolframAlpha';
+ ok vecnone {factorion($_)} 146 .. 40584;
+ ok factorion(25, 6), '25 = 41(6) = 4! + 1!';
+ ok factorion(26, 6), '26 = 42(6) = 4! + 2!';
+ ok factorion(49, 5), '49 = 144(5) = 1! + 4! + 4!';
+ }
+
+ done_testing;
+ exit;
+}