aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-108/perlboy1967/perl/ch-2.pl42
1 files changed, 25 insertions, 17 deletions
diff --git a/challenge-108/perlboy1967/perl/ch-2.pl b/challenge-108/perlboy1967/perl/ch-2.pl
index 7a241b6215..8e06512db9 100755
--- a/challenge-108/perlboy1967/perl/ch-2.pl
+++ b/challenge-108/perlboy1967/perl/ch-2.pl
@@ -6,32 +6,40 @@
# Task 2 - Bell Numbers
#
# Author: Niels 'PerlBoy' van Dijke
+#
+# Using the 'Bell triangle' algorithm
use v5.16;
use strict;
use warnings;
-my @bn = (1);
-
-# Using the 'Bell triangle' algorithm
+# To have integer results over nB > 26 on my machine:
+use bigint;
-my $bnT = [[1]];
+my $nB = shift // 10;
-foreach my $bTRidx (1..8) {
- my $prevTRdim = scalar(@{$bnT->[$bTRidx-1]});
+my @bn = (1,1);
+my @bnT = map {[$_]} @bn;
- # Copy
- $bnT->[$bTRidx][0] = $bnT->[$bTRidx-1][$prevTRdim-1];
- foreach my $bTCidx (1 .. $prevTRdim) {
+if ($nB > 2) {
+ foreach (2 .. $nB-1) {
+ # Add row
+ push(@bnT,[0]);
- # Add
- $bnT->[$bTRidx][$bTCidx] =
- $bnT->[$bTRidx-1][$bTCidx-1] +
- $bnT->[$bTRidx][$bTCidx-1];
- }
+ # Copy
+ $bnT[-1][0] = $bnT[-2][-1];
+ foreach (1 .. scalar(@{$bnT[-2]})) {
+ # Add
+ $bnT[-1][$_] =
+ $bnT[-2][$_-1] +
+ $bnT[-1][$_-1];
+ }
- push(@bn, $bnT->[-1][-1]);
-}
+ push(@bn,$bnT[-1][-1]);
+ shift(@bnT);
+ }
+}
# Print
-printf "%s\n", join(' ',1,@bn);
+my $i = 0;
+printf "%s\n", join("\n",map{++$i; "$i: $_"} @bn[0 .. $nB-1]);