diff options
| author | Abigail <abigail@abigail.be> | 2021-04-12 20:03:28 +0200 |
|---|---|---|
| committer | Abigail <abigail@abigail.be> | 2021-04-12 22:17:36 +0200 |
| commit | fa485e6665431156c33d7c7ad3fa17be1b54c596 (patch) | |
| tree | fa2f19603398f244714c1c1478dc6582473aa797 /challenge-108/abigail/perl | |
| parent | f02c5746861535f4471879d33ff63759bbcd6e9a (diff) | |
| download | perlweeklychallenge-club-fa485e6665431156c33d7c7ad3fa17be1b54c596.tar.gz perlweeklychallenge-club-fa485e6665431156c33d7c7ad3fa17be1b54c596.tar.bz2 perlweeklychallenge-club-fa485e6665431156c33d7c7ad3fa17be1b54c596.zip | |
Perl solution for week 108, part 2
Diffstat (limited to 'challenge-108/abigail/perl')
| -rw-r--r-- | challenge-108/abigail/perl/ch-2.pl | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/challenge-108/abigail/perl/ch-2.pl b/challenge-108/abigail/perl/ch-2.pl new file mode 100644 index 0000000000..d89e32391e --- /dev/null +++ b/challenge-108/abigail/perl/ch-2.pl @@ -0,0 +1,68 @@ +#!/opt/perl/bin/perl + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +use experimental 'signatures'; +use experimental 'lexical_subs'; + +# +# See ../README.md +# + +# +# Run as: perl ch-2.pl [plain|fetch|compute] +# +# +# Three solutions, depending on the command line argument: +# 1) We just print the first 10 Bell numbers. The simplest solution. +# 2) We fetch the first 10 Bell numbers from the OEIS. +# 3) We calculate the Bell numbers from the Bell triangle. +# + +my $COUNT = 10; + +my $TYPE_PLAIN = 0; +my $TYPE_FETCH = 1; +my $TYPE_COMPUTE = 2; + +my $type = $TYPE_PLAIN; # Default + $type = $TYPE_FETCH if @ARGV && $ARGV [0] eq "fetch"; + $type = $TYPE_COMPUTE if @ARGV && $ARGV [0] eq "compute"; + + +if ($type == $TYPE_PLAIN) { + say "1, 1, 2, 5, 15, 52, 203, 877, 4140, 21147"; +} +elsif ($type == $TYPE_FETCH) { + use OEIS; + $, = ", "; + say oeis (110 => 10) +} +elsif ($type == $TYPE_COMPUTE) { + my $COUNT = 10; + + # + # Create Bell triangle (See https://en.wikipedia.org/wiki/Bell_triangle) + # The Bell numbers (starting from the second one), are on the + # right diagonal. + # + # The Bell triangle is defined as follows: + # Bell (0, 0) = 1 + # Bell (n, 0) = Bell (n - 1, n - 1), k > 0 + # Bell (n, m) = Bell (n, m - 1) + Bell (n - 1, m - 1), n > 0, m > 0 + # + my @bell = [1]; + for (my $x = 1; $x < $COUNT - 1; $x ++) { + $bell [$x] [0] = $bell [$x - 1] [-1]; + for (my $y = 1; $y <= $x; $y ++) { + $bell [$x] [$y] = $bell [$x] [$y - 1] + $bell [$x - 1] [$y - 1] + } + } + + $, = ", "; + say 1, map {$$_ [-1]} @bell; +} |
