diff options
| author | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2022-02-21 14:04:05 +0100 |
|---|---|---|
| committer | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2022-02-25 18:10:55 +0100 |
| commit | b56cd15700c9b3cea23acef89aa95ad257b2f175 (patch) | |
| tree | 9a66446172149b835bf04bb3d74eda43950bcb17 | |
| parent | f497ae45aa5090789913209074b22a085c427849 (diff) | |
| download | perlweeklychallenge-club-b56cd15700c9b3cea23acef89aa95ad257b2f175.tar.gz perlweeklychallenge-club-b56cd15700c9b3cea23acef89aa95ad257b2f175.tar.bz2 perlweeklychallenge-club-b56cd15700c9b3cea23acef89aa95ad257b2f175.zip | |
Solution to task 2
| -rwxr-xr-x | challenge-153/jo-37/perl/ch-2.pl | 66 |
1 files changed, 66 insertions, 0 deletions
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; +} |
