diff options
| author | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2022-02-25 18:11:43 +0100 |
|---|---|---|
| committer | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2022-02-25 18:11:43 +0100 |
| commit | 0c8816efabaee1e29081d1698bc00bc78f439ee8 (patch) | |
| tree | 9a66446172149b835bf04bb3d74eda43950bcb17 | |
| parent | 50b018e2e1f73a72d4564dbe5002a304f4b8464e (diff) | |
| parent | b56cd15700c9b3cea23acef89aa95ad257b2f175 (diff) | |
| download | perlweeklychallenge-club-0c8816efabaee1e29081d1698bc00bc78f439ee8.tar.gz perlweeklychallenge-club-0c8816efabaee1e29081d1698bc00bc78f439ee8.tar.bz2 perlweeklychallenge-club-0c8816efabaee1e29081d1698bc00bc78f439ee8.zip | |
Solutions to challenge 153
| -rwxr-xr-x | challenge-153/jo-37/perl/ch-1.pl | 69 | ||||
| -rwxr-xr-x | challenge-153/jo-37/perl/ch-2.pl | 66 |
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; +} |
