factorial_expansion.sf 956 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #!/usr/bin/ruby
  2. # Generate the factorial expansion of a given real constant.
  3. # Such that:
  4. # x = Sum_{k>=0} f(k) / k!
  5. # See also:
  6. # https://oeis.org/A067882
  7. # https://oeis.org/A096622
  8. func f(n, x, r=:floor) {
  9. return x.(r) if (n == 0)
  10. (x * n!).(r) - n*((n-1)! * x).(r)
  11. }
  12. var x = Num.EulerGamma
  13. var a = 30.of { f(_, x) }
  14. var b = 30.of { f(_, x, :round) }
  15. say "const: #{x}"
  16. say ("floor: ", a.len.range.sum { |k|
  17. a[k] / k!
  18. })
  19. say ("round: ", b.len.range.sum { |k|
  20. b[k] / k!
  21. })
  22. say ''
  23. say "floor: #{a}"
  24. say "round: #{b}"
  25. __END__
  26. const: 0.57721566490153286060651209008240243104215933594
  27. floor: 0.577215664901532860606512090082341769080234288111
  28. round: 0.57721566490153286060651209008245486870909876528
  29. 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]
  30. 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]