-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactorial_expansion.sf
42 lines (30 loc) · 956 Bytes
/
factorial_expansion.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
42
#!/usr/bin/ruby
# Generate the factorial expansion of a given real constant.
# Such that:
# x = Sum_{k>=0} f(k) / k!
# See also:
# https://oeis.org/A067882
# https://oeis.org/A096622
func f(n, x, r=:floor) {
return x.(r) if (n == 0)
(x * n!).(r) - n*((n-1)! * x).(r)
}
var x = Num.EulerGamma
var a = 30.of { f(_, x) }
var b = 30.of { f(_, x, :round) }
say "const: #{x}"
say ("floor: ", a.len.range.sum { |k|
a[k] / k!
})
say ("round: ", b.len.range.sum { |k|
b[k] / k!
})
say ''
say "floor: #{a}"
say "round: #{b}"
__END__
const: 0.57721566490153286060651209008240243104215933594
floor: 0.577215664901532860606512090082341769080234288111
round: 0.57721566490153286060651209008245486870909876528
floor: [0, 0, 1, 0, 1, 4, 1, 4, 1, 3, 0, 2, 3, 0, 5, 14, 12, 16, 14, 7, 13, 18, 17, 19, 11, 22, 13, 13, 26, 12]
round: [1, 0, -1, 0, 2, -1, 2, -3, 1, 3, 0, 2, 3, 0, 6, 0, -3, 0, -4, 8, -6, -2, -4, -4, 12, -2, -12, -13, -2, 13]