June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue