How to resolve the algorithm Bernoulli numbers step by step in the Raku programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Bernoulli numbers step by step in the Raku 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 Raku programming language
Source code in the raku programming language
sub bernoulli($n) {
my @a;
for 0..$n -> $m {
@a[$m] = FatRat.new(1, $m + 1);
for reverse 1..$m -> $j {
@a[$j - 1] = $j * (@a[$j - 1] - @a[$j]);
}
}
return @a[0];
}
constant @bpairs = grep *.value.so, ($_ => bernoulli($_) for 0..60);
my $width = max @bpairs.map: *.value.numerator.chars;
my $form = "B(%2d) = \%{$width}d/%d\n";
printf $form, .key, .value.nude for @bpairs;
constant bernoulli = gather {
my @a;
for 0..* -> $m {
@a = FatRat.new(1, $m + 1),
-> $prev {
my $j = @a.elems;
$j * (@a.shift - $prev);
} ... { not @a.elems }
take $m => @a[*-1] if @a[*-1];
}
}
constant @bpairs = bernoulli[^52];
my $width = max @bpairs.map: *.value.numerator.chars;
my $form = "B(%d)\t= \%{$width}d/%d\n";
printf $form, .key, .value.nude for @bpairs;
sub infix:(\prev, \this) {
this.key => this.key * (this.value - prev.value)
}
sub next-bernoulli ( (:key($pm), :value(@pa)) ) {
$pm + 1 => [
map *.value,
[\bop] ($pm + 2 ... 1) Z=> FatRat.new(1, $pm + 2), |@pa
]
}
constant bernoulli =
grep *.value,
map { .key => .value[*-1] },
(0 => [FatRat.new(1,1)], &next-bernoulli ... *)
;
constant @bpairs = bernoulli[^52];
my $width = max @bpairs.map: *.value.numerator.chars;
my $form = "B(%d)\t= \%{$width}d/%d\n";
printf $form, .key, .value.nude for @bpairs;
You may also check:How to resolve the algorithm Hunt the Wumpus step by step in the C++ programming language
You may also check:How to resolve the algorithm Tokenize a string step by step in the Falcon programming language
You may also check:How to resolve the algorithm Self numbers step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Get system command output step by step in the F# programming language
You may also check:How to resolve the algorithm Averages/Simple moving average step by step in the CoffeeScript programming language