bernoulli_numbers_recurrence_2.sf 628 B

1234567891011121314151617181920212223242526272829
  1. #!/usr/bin/ruby
  2. # Daniel "Trizen" Șuteu
  3. # Date: 24 February 2018
  4. # https://github.com/trizen
  5. # A new recurrence for computing Bernoulli numbers.
  6. # Formula:
  7. # a(0) = 1
  8. # a(n) = 1 - ((Sum_{k=0..n-1} a(k) * binomial(n + 1, k)) / (n+1))
  9. # Which gives us the nth-Bernoulli number, B_n, as:
  10. # B_n = a(n)
  11. # See also:
  12. # https://en.wikipedia.org/wiki/Bernoulli_number
  13. # https://arxiv.org/pdf/0708.0809.pdf (Compositional Bernoulli numbers)
  14. func a((0)) { 1 }
  15. func a(n) is cached {
  16. 1 - (sum(^n, {|k| a(k) * binomial(n+1, k) }) / (n+1))
  17. }
  18. for n in (0..60 `by` 2) {
  19. printf("B(%2d) = %50s / %s\n", n, a(n) -> nude)
  20. }