diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-03-21 07:59:18 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-03-21 07:59:18 +0100 |
| commit | ffc60b8def48fc2b6b55b063aa956236e99d65b9 (patch) | |
| tree | d2d2c01fd51c8cb00ccb6121c484c8a5411cca44 | |
| parent | 19750d81e7350048af6c55560ead52cad5e2eebc (diff) | |
| parent | a4d9d6b08674e243d74f35ab1fa84bf2a48015af (diff) | |
| download | perlweeklychallenge-club-ffc60b8def48fc2b6b55b063aa956236e99d65b9.tar.gz perlweeklychallenge-club-ffc60b8def48fc2b6b55b063aa956236e99d65b9.tar.bz2 perlweeklychallenge-club-ffc60b8def48fc2b6b55b063aa956236e99d65b9.zip | |
Merge pull request #3743 from jo-37/contrib
avoid elements outside pascal's triangle
| -rwxr-xr-x | challenge-104/jo-37/perl/ch-1.pl | 9 |
1 files changed, 7 insertions, 2 deletions
diff --git a/challenge-104/jo-37/perl/ch-1.pl b/challenge-104/jo-37/perl/ch-1.pl index 83e366daa3..1b33496b62 100755 --- a/challenge-104/jo-37/perl/ch-1.pl +++ b/challenge-104/jo-37/perl/ch-1.pl @@ -3,6 +3,7 @@ use v5.16; use Test2::V0; use Math::Prime::Util 'binomial'; +use Math::Utils 'ceil'; use List::Util 'reduce'; use experimental 'signatures'; @@ -34,7 +35,7 @@ EOS ### Input and Output -my $n = shift || 50; +my $n = shift // 50; if ($fusc) { say "fusc($n)=" x !!$verbose, fusc($n); } else { @@ -47,9 +48,13 @@ if ($fusc) { # Non-recursive implementation of fusc according to # http://oeis.org/A002487 sub fusc ($n) { - reduce {$a += $b % 2} 0, map binomial($_, $n - $_ - 1), 0 .. $n - 1; + # Interestingly, without the modulus this would produce the + # respective Fibonacci number. + reduce {$a += $b % 2} 0, map binomial($_, $n - $_ - 1), + ceil(($n - 1) / 2) .. $n - 1; } + ### Tests sub run_tests { |
