generalized_expansion.sf 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #!/usr/bin/ruby
  2. # Generalized expansion of a given real constant, given a function t(k).
  3. # Such that:
  4. # x = Sum_{k>=0} f(k) / t(k)
  5. # See also:
  6. # https://oeis.org/A067882
  7. # https://oeis.org/A096622
  8. local Num!PREC = 300.numify
  9. func f(n, x, t, r={.floor}) {
  10. return r(x) if (n == 0)
  11. r(x * t(n)) - (t(n)/t(n-1) * r(t(n-1) * x))
  12. }
  13. var t = { .superfactorial }
  14. var x = Num.e
  15. var a = 15.of { f(_, x, t) }
  16. var b = 15.of { f(_, x, t, {.round}) }
  17. say "const: #{x}"
  18. say ("floor: ", a.len.range.sum { |k|
  19. a[k] / t(k)
  20. })
  21. say ("round: ", b.len.range.sum { |k|
  22. b[k] / t(k)
  23. })
  24. say ''
  25. say "floor: #{a}"
  26. say "round: #{b}"
  27. __END__
  28. const: 2.71828182845904523536028747135266249775724709369995957496696762772407663035
  29. floor: 2.71828182845904523536028747135266249775724709369995957496696762771492315184
  30. round: 2.71828182845904523536028747135266249775724709369995957496696762772939204445
  31. floor: [2, 0, 1, 2, 14, 103, 590, 1985, 12783, 261726, 3567625, 4152491, 421616733, 2483567501, 21279242709]
  32. round: [3, 0, -1, 3, -9, -16, -130, 1985, 12784, -101153, -61175, 4152492, -57384867, 2483567501, 21279242710]