June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -28,5 +28,5 @@ The Akiyama–Tanigawa algorithm for the "second Bernoulli numbers" as taken fro
|
|||
* Sequence [http://oeis.org/A027641 A027641 Numerator of Bernoulli number B_n] on The On-Line Encyclopedia of Integer Sequences.
|
||||
* Sequence [http://oeis.org/A027642 A027642 Denominator of Bernoulli number B_n] on The On-Line Encyclopedia of Integer Sequences.
|
||||
* Entry [http://mathworld.wolfram.com/BernoulliNumber.html Bernoulli number] on The Eric Weisstein's World of Mathematics (TM).
|
||||
* Luschny's [http://luschny.de/math/zeta/The-Bernoulli-Manifesto.html The Bernoulli Manifesto] for a discussion on <math>B_1</math> = -½ vs. +½.
|
||||
* Luschny's [http://luschny.de/math/zeta/The-Bernoulli-Manifesto.html The Bernoulli Manifesto] for a discussion on <big> '''B<sub>1</sub> = -½''' versus '''+½'''. </big>
|
||||
<br><br>
|
||||
|
|
|
|||
39
Task/Bernoulli-numbers/C++/bernoulli-numbers.cpp
Normal file
39
Task/Bernoulli-numbers/C++/bernoulli-numbers.cpp
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
/**
|
||||
* Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
|
||||
* Apple LLVM version 9.1.0 (clang-902.0.39.1)
|
||||
* Target: x86_64-apple-darwin17.5.0
|
||||
* Thread model: posix
|
||||
*/
|
||||
|
||||
#include <iostream> //std::cout
|
||||
#include <iostream> //formatting
|
||||
#include <vector> //Container
|
||||
#include <boost/rational.hpp> // Rationals
|
||||
#include <boost/multiprecision/cpp_int.hpp> //1024bit precision
|
||||
|
||||
|
||||
typedef boost::rational<boost::multiprecision::int1024_t> rational; // reduce boilerplate
|
||||
|
||||
rational bernulli(size_t n){
|
||||
|
||||
auto out = std::vector<rational>();
|
||||
|
||||
for(size_t m=0;m<=n;m++){
|
||||
out.emplace_back(1,(m+1)); // automatically constructs object
|
||||
for (size_t j = m;j>=1;j--){
|
||||
out[j-1] = rational(j) * (out[j-1]-out[j]);
|
||||
}
|
||||
}
|
||||
return out[0];
|
||||
}
|
||||
|
||||
int main() {
|
||||
for(size_t n = 0; n <= 60;n+=n>=2?2:1){
|
||||
auto b = bernulli(n);
|
||||
std::cout << "B("<<std::right<<std::setw(2)<<n<<") = ";
|
||||
std::cout << std::right<<std::setw(44)<<b.numerator();
|
||||
std::cout << " / " << b.denominator() <<std::endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -1,62 +1,27 @@
|
|||
# Taken from the 'Ada 99' project, https://marquisdegeek.com/code_ada99
|
||||
require "big"
|
||||
|
||||
class Fraction
|
||||
def initialize(n : Int64, d : Int64)
|
||||
@numerator = n
|
||||
@denominator = d
|
||||
class Bernoulli
|
||||
include Iterator(Tuple(Int32, BigRational))
|
||||
|
||||
def initialize
|
||||
@a = [] of BigRational
|
||||
@m = 0
|
||||
end
|
||||
|
||||
def numerator
|
||||
@numerator
|
||||
end
|
||||
|
||||
def denominator
|
||||
@denominator
|
||||
end
|
||||
|
||||
def subtract(rhs_fraction)
|
||||
rhs_numerator = rhs_fraction.numerator * @denominator
|
||||
rhs_denominator = rhs_fraction.denominator * @denominator
|
||||
@numerator *= rhs_fraction.denominator
|
||||
@denominator *= rhs_fraction.denominator
|
||||
@numerator -= rhs_numerator
|
||||
self.reduce
|
||||
end
|
||||
|
||||
def multiply(value)
|
||||
@numerator *= value
|
||||
end
|
||||
|
||||
def reduce
|
||||
gcd = gcd(@numerator, @denominator)
|
||||
@numerator /= gcd
|
||||
@denominator /= gcd
|
||||
end
|
||||
|
||||
def to_s
|
||||
@numerator == 0 ? 0 : @numerator.to_s + '/' + @denominator.to_s
|
||||
def next
|
||||
@a << BigRational.new(1, @m+1)
|
||||
@m.downto(1) { |j| @a[j-1] = j*(@a[j-1] - @a[j]) }
|
||||
v = @m.odd? && @m != 1 ? BigRational.new(0, 1) : @a.first
|
||||
return {@m, v}
|
||||
ensure
|
||||
@m += 1
|
||||
end
|
||||
end
|
||||
|
||||
def gcd(a, b)
|
||||
# we need b>0 because b on its own isn't considered true
|
||||
b > 0 ? gcd(b, a % b) : a
|
||||
end
|
||||
b = Bernoulli.new
|
||||
bn = b.first(61).to_a
|
||||
|
||||
def calculate_bernoulli(bern)
|
||||
row = [] of Fraction
|
||||
0_i64.step(bern) do |m|
|
||||
row << Fraction.new(1_i64, m + 1)
|
||||
m.step(1, -1) do |j|
|
||||
row[j - 1].subtract(row[j])
|
||||
row[j - 1].multiply(j)
|
||||
row[j - 1].reduce
|
||||
end
|
||||
end
|
||||
|
||||
row[0]
|
||||
end
|
||||
|
||||
1_i64.step(30_i64) do |bern|
|
||||
puts "#{bern} : #{calculate_bernoulli(bern).to_s}"
|
||||
max_width = bn.map { |_, v| v.numerator.to_s.size }.max
|
||||
bn.reject { |i, v| v.zero? }.each do |i, v|
|
||||
puts "B(%2i) = %*i/%i" % [i, max_width, v.numerator, v.denominator]
|
||||
end
|
||||
|
|
|
|||
|
|
@ -14,3 +14,10 @@ constant bernoulli =
|
|||
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;
|
||||
|
|
|
|||
|
|
@ -42,7 +42,14 @@ comb: procedure expose !.; parse arg x,y; if x==y then return 1
|
|||
gcd: procedure; parse arg x,y; x=abs(x)
|
||||
do until y==0; parse value x//y y with y x; end; return x
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
lcm: procedure; parse arg x,y; x=abs(x); return x*y/gcd(x,y)
|
||||
lcm: procedure; parse arg x,y; if x<0 then x=-x
|
||||
if y<0 then y= -y
|
||||
if y==0 then return 0 /*if zero, then LCM is also zero. */
|
||||
d=x*y /*calculate part of the LCM here. */
|
||||
do until y==0; parse value x//y y with y x
|
||||
end /*until*/ /* [↑] this is a short & fast GCD*/
|
||||
x=d%x /*divide the pre─calculated value.*/
|
||||
return x /*return with the LCM of the args.*/
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
perm: procedure expose !.; parse arg x,y; if !.p.x.y\==0 then return !.p.x.y
|
||||
z=1; do j=x-y+1 to x; z=z*j; end; !.p.x.y=z; return z
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
' Bernoulli numbers - vb.net - 06/03/2017
|
||||
Imports System.Numerics 'BinInteger
|
||||
Imports System.Numerics 'BigInteger
|
||||
|
||||
Module Bernoulli_numbers
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue