12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- #!/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]
|