diff options
| author | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2022-02-21 13:43:02 +0100 |
|---|---|---|
| committer | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2022-02-25 18:10:53 +0100 |
| commit | f497ae45aa5090789913209074b22a085c427849 (patch) | |
| tree | 068c9111b70c10d59bebf2034f0c22a17dbbc7a2 | |
| parent | 50b018e2e1f73a72d4564dbe5002a304f4b8464e (diff) | |
| download | perlweeklychallenge-club-f497ae45aa5090789913209074b22a085c427849.tar.gz perlweeklychallenge-club-f497ae45aa5090789913209074b22a085c427849.tar.bz2 perlweeklychallenge-club-f497ae45aa5090789913209074b22a085c427849.zip | |
Solution to task 1
| -rwxr-xr-x | challenge-153/jo-37/perl/ch-1.pl | 69 |
1 files changed, 69 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; +} |
