-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbernoulli_approx.sf
41 lines (32 loc) · 1.21 KB
/
bernoulli_approx.sf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/ruby
# Daniel "Trizen" Șuteu
# License: GPLv3
# Date: 21 December 2016
# https://github.com/trizen
# Two formulas for the nth Bernoulli number.
define π = Num.pi
#
## B_n = (-1)^(-n/2) * (-2)^(1 - n) * pi^(-n) * n! * zeta(n)
#
func bern_float(n) {
pow(-1, n/2) * pow(-2, 1-n) * pow(π, -n) * Γ(n+1) * ζ(n)
}
#
## B_n ~ -4 * (-1)^(-n/2) * exp(-n) * (n/2)^(n+1/2) * pi^(1/2 - n)
#
func bern_approx(n) {
-4 * pow(-1, -n/2) * exp(-n) * pow(n/2, n + 1/2) * pow(π, 1/2 - n)
}
say "B(100) = #{bern(100)}"
say "B(100) ≈ #{bern_float(100)}"
say "B(100) ~ #{bern_approx(100)}"
say "\n=> Non-integers:"
say "B(13.456) ≈ #{bern_float(13.456)}"
say "B(13.456) ~ #{bern_approx(13.456)}"
__END__
B(100) = -2838224957069370695926415633648176473828468092801288212822853171446486511107028.134143414341434143414341434143414341434143414341
B(100) ≈ -2.83822495706937069592641563364817647382846809280e78
B(100) ~ -2.83586076270339739802094973505745135350946659331e78
=> Non-integers:
B(13.456) ≈ 0.49103883572658300550097232792093938650387718835+0.564079736114738926156801453364729246710434799152i
B(13.456) ~ 0.487964159607887905546557272273095918447566234328+0.560547708976589026486704805106543962108358656388i