96 lines
1.9 KiB
Text
96 lines
1.9 KiB
Text
module Bernoulli_Numbers {
|
|
Class Rational {
|
|
numerator as decimal, denominator as decimal
|
|
gcd=lambda->0
|
|
lcm=lambda->0
|
|
operator "+" {
|
|
Read l
|
|
denom=.lcm(l.denominator, .denominator)
|
|
.numerator<=denom/l.denominator*l.numerator+denom/.denominator*.numerator
|
|
if .numerator==0 then denom=1
|
|
.denominator<=denom
|
|
}
|
|
Operator Unary {
|
|
.numerator-!
|
|
}
|
|
Operator "-" {
|
|
Read l
|
|
Call Operator "+", -l
|
|
}
|
|
Group Real {
|
|
value {
|
|
link parent numerator, denominator to n, d
|
|
=n/d
|
|
}
|
|
}
|
|
Group ToString$ {
|
|
value {
|
|
link parent numerator, denominator to n, d
|
|
=Str$(n)+"/"+Str$(d,"")
|
|
}
|
|
}
|
|
class:
|
|
Module Rational (.numerator, .denominator) {
|
|
if .denominator=0 then .denominator<=1
|
|
while frac(.numerator)<>0 {
|
|
.numerator*=10@
|
|
.denominator*=10@
|
|
}
|
|
sgn=Sgn(.numerator)*Sgn(.denominator)
|
|
.denominator<=abs(.denominator)
|
|
.numerator<=abs(.numerator)*sgn
|
|
gcd1=lambda (a as decimal, b as decimal) -> {
|
|
if a<b then swap a,b
|
|
g=a mod b
|
|
while g {
|
|
a=b:b=g: g=a mod b
|
|
}
|
|
=abs(b)
|
|
}
|
|
gdcval=gcd1(abs(.numerator), .denominator)
|
|
if gdcval<.denominator and gdcval<>0 then
|
|
.denominator/=gdcval
|
|
.numerator/=gdcval
|
|
end if
|
|
.gcd<=gcd1
|
|
.lcm<=lambda gcd=gcd1 (a as decimal, b as decimal) -> {
|
|
=a/gcd(a,b)*b
|
|
}
|
|
}
|
|
}
|
|
Open "out.txt" for output as #f
|
|
r1=Rational(1,1)
|
|
b=0
|
|
nextBern()
|
|
b=1
|
|
nextBern()
|
|
for b=2 to 28 step 2
|
|
nextBern()
|
|
next
|
|
close #f
|
|
|
|
sub nextBern()
|
|
local m=@bernoulli(b)
|
|
Print #F, format$("B({0::-2})={1}",b, m.tostring$)
|
|
end sub
|
|
function bernoulli(n as decimal)
|
|
Local a(1 to n+1)=Rational(1,1), z
|
|
local decimal m, j
|
|
for m=0 to n
|
|
a(m + 1) = Rational(1 , m + 1)
|
|
if m<1 then continue
|
|
for j = m to 1
|
|
z= a(J) - a(j+1) + r1
|
|
a(j)=z
|
|
nn=z.gcd(j, z.denominator)
|
|
a(j).numerator*=j/nn
|
|
a(j).denominator/=nn
|
|
next
|
|
for a(1) {
|
|
this=rational(.numerator, .denominator)
|
|
}
|
|
next
|
|
=a(1)
|
|
end function
|
|
}
|
|
Bernoulli_Numbers
|