In mathematics,   Faulhaber's formula,   named after Johann Faulhaber,   expresses the sum of the p-th powers of the first n positive integers as a (p + 1)th-degree polynomial function of n,   the coefficients involving Bernoulli numbers.



Task

Generate the first 10 closed-form expressions, starting with p = 0.



Related tasks

   Bernoulli numbers.
   evaluate binomial coefficients.


See also

   The Wikipedia entry:   Faulhaber's formula.
   The Wikipedia entry:   Bernoulli numbers.
   The Wikipedia entry:   binomial coefficients.


const AnyNum = require('Math::AnyNum')
const Poly   = require('Math::Polynomial')
 
Poly.string_config(Hash(
    fold_sign => true, prefix => "",
    suffix => "", variable => "n"
))
 
func anynum(n) {
    AnyNum.new(n.as_rat)
}
 
func faulhaber_formula(p) {
    (p+1).of { |j|
        Poly.monomial(p - j + 1)\
            .mul_const(anynum(bernoulli(j)))\
            .mul_const(anynum(binomial(p+1, j)))
    }.reduce(:add).div_const(p+1)
}
 
for p in (^10) {
    printf("%2d: %s\n", p, faulhaber_formula(p))
}