How to resolve the algorithm Bernoulli numbers step by step in the Perl programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Bernoulli numbers step by step in the Perl programming language
Table of Contents
Problem Statement
Bernoulli numbers are used in some series expansions of several functions (trigonometric, hyperbolic, gamma, etc.), and are extremely important in number theory and analysis. Note that there are two definitions of Bernoulli numbers; this task will be using the modern usage (as per The National Institute of Standards and Technology convention). The nth Bernoulli number is expressed as Bn.
The Akiyama–Tanigawa algorithm for the "second Bernoulli numbers" as taken from wikipedia is as follows:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Bernoulli numbers step by step in the Perl programming language
Source code in the perl programming language
#!perl
use strict;
use warnings;
use List::Util qw(max);
use Math::BigRat;
my $one = Math::BigRat->new(1);
sub bernoulli_print {
my @a;
for my $m ( 0 .. 60 ) {
push @a, $one / ($m + 1);
for my $j ( reverse 1 .. $m ) {
# This line:
( $a[$j-1] -= $a[$j] ) *= $j;
# is a faster version of the following line:
# $a[$j-1] = $j * ($a[$j-1] - $a[$j]);
# since it avoids unnecessary object creation.
}
next unless $a[0];
printf "B(%2d) = %44s/%s\n", $m, $a[0]->parts;
}
}
bernoulli_print();
use ntheory qw/bernfrac/;
for my $n (0 .. 60) {
my($num,$den) = bernfrac($n);
printf "B(%2d) = %44s/%s\n", $n, $num, $den if $num != 0;
}
use Math::Pari qw/bernfrac/;
for my $n (0 .. 60) {
my($num,$den) = split "/", bernfrac($n);
printf("B(%2d) = %44s/%s\n", $n, $num, $den||1) if $num != 0;
}
You may also check:How to resolve the algorithm Cumulative standard deviation step by step in the E programming language
You may also check:How to resolve the algorithm Display a linear combination step by step in the Lambdatalk programming language
You may also check:How to resolve the algorithm Self-describing numbers step by step in the K programming language
You may also check:How to resolve the algorithm Execute a system command step by step in the Emacs Lisp programming language
You may also check:How to resolve the algorithm Pseudo-random numbers/Combined recursive generator MRG32k3a step by step in the Perl programming language