aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-04-16 05:41:18 +0100
committerGitHub <noreply@github.com>2021-04-16 05:41:18 +0100
commit0312c9a6056055faef87eacdc899165eaf6b05f2 (patch)
tree690b90a7e690bcb4ca6c4fb32cf6810f4adc9b69
parent25b0fd4b64627633063974213c5394c060bc9003 (diff)
parent783d6425052d04b539e5c5e1818b3c3091bcc4f5 (diff)
downloadperlweeklychallenge-club-0312c9a6056055faef87eacdc899165eaf6b05f2.tar.gz
perlweeklychallenge-club-0312c9a6056055faef87eacdc899165eaf6b05f2.tar.bz2
perlweeklychallenge-club-0312c9a6056055faef87eacdc899165eaf6b05f2.zip
Merge pull request #3898 from PerlBoy1967/branch-for-challenge-108
I realized the code wasn't as clean as I wanted it to be.
-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]);