diff options
| author | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2021-04-12 09:41:30 +0200 |
|---|---|---|
| committer | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2021-04-12 09:55:57 +0200 |
| commit | 8a7cfff62073293378e617fbbb740b7ab4da8c3f (patch) | |
| tree | 8f068916e217fccdff00a5e0c1058b678baad895 | |
| parent | 4310aa2243ac7f564b66ec8ec146dc8f95ab40bc (diff) | |
| download | perlweeklychallenge-club-8a7cfff62073293378e617fbbb740b7ab4da8c3f.tar.gz perlweeklychallenge-club-8a7cfff62073293378e617fbbb740b7ab4da8c3f.tar.bz2 perlweeklychallenge-club-8a7cfff62073293378e617fbbb740b7ab4da8c3f.zip | |
Solution to task 2
| -rwxr-xr-x | challenge-108/jo-37/perl/ch-2.pl | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/challenge-108/jo-37/perl/ch-2.pl b/challenge-108/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..fb28a5f33a --- /dev/null +++ b/challenge-108/jo-37/perl/ch-2.pl @@ -0,0 +1,47 @@ +#!/usr/bin/perl -s + +use v5.16; +use Test2::V0; +use Math::Prime::Util qw(vecsum stirling); + +our $tests; + +run_tests() if $tests; # does not return + +die <<EOS unless @ARGV == 1; +usage: $0 [-tests] [num] + +-tests + run some tests + +num + print first <num> Bell numbers. + +EOS + + +### Input and Output + +say "$_ ", B($_) for 0 .. $ARGV[0] - 1; + + +### Implementation + +# Facing this task after having played with Math::Prime::Util for some +# weeks, the module shouts: "Use me!" And in fact: The Bell numbers +# appear as an example. This looks like a one week leave. +# Taking a verbatim copy from the module's POD. + +sub B { my $n = shift; vecsum(map { stirling($n,$_,2) } 0..$n) } + +### Examples and tests + +sub run_tests { + my $i = 0; + for (1, 1, 2, 5, 15, 52, 203, 877, 4140, 21147) { + is B($i++), $_; + } + + done_testing; + exit; +} |
