Faulhaber's formula: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
(→‎{{header|Sidef}}: switched to built-in polynomials)
Tag: Made through Tor
Line 2,824: Line 2,824:


=={{header|Sidef}}==
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">const AnyNum = require('Math::AnyNum')
<syntaxhighlight lang="ruby">func faulhaber_formula(p) {
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|
(p+1).of { |j|
Poly.monomial(p - j + 1)\
Poly(p - j + 1 => 1) * bernoulli(j) * binomial(p+1, j)
}.sum / (p+1)
.mul_const(anynum(bernoulli(j)))\
.mul_const(anynum(binomial(p+1, j)))
}.reduce(:add).div_const(p+1)
}
}


Line 2,849: Line 2,835:
{{out}}
{{out}}
<pre>
<pre>
0: n
0: x
1: 1/2 n^2 + 1/2 n
1: 1/2*x^2 + 1/2*x
2: 1/3 n^3 + 1/2 n^2 + 1/6 n
2: 1/3*x^3 + 1/2*x^2 + 1/6*x
3: 1/4 n^4 + 1/2 n^3 + 1/4 n^2
3: 1/4*x^4 + 1/2*x^3 + 1/4*x^2
4: 1/5 n^5 + 1/2 n^4 + 1/3 n^3 - 1/30 n
4: 1/5*x^5 + 1/2*x^4 + 1/3*x^3 - 1/30*x
5: 1/6 n^6 + 1/2 n^5 + 5/12 n^4 - 1/12 n^2
5: 1/6*x^6 + 1/2*x^5 + 5/12*x^4 - 1/12*x^2
6: 1/7 n^7 + 1/2 n^6 + 1/2 n^5 - 1/6 n^3 + 1/42 n
6: 1/7*x^7 + 1/2*x^6 + 1/2*x^5 - 1/6*x^3 + 1/42*x
7: 1/8 n^8 + 1/2 n^7 + 7/12 n^6 - 7/24 n^4 + 1/12 n^2
7: 1/8*x^8 + 1/2*x^7 + 7/12*x^6 - 7/24*x^4 + 1/12*x^2
8: 1/9 n^9 + 1/2 n^8 + 2/3 n^7 - 7/15 n^5 + 2/9 n^3 - 1/30 n
8: 1/9*x^9 + 1/2*x^8 + 2/3*x^7 - 7/15*x^5 + 2/9*x^3 - 1/30*x
9: 1/10 n^10 + 1/2 n^9 + 3/4 n^8 - 7/10 n^6 + 1/2 n^4 - 3/20 n^2
9: 1/10*x^10 + 1/2*x^9 + 3/4*x^8 - 7/10*x^6 + 1/2*x^4 - 3/20*x^2
</pre>
</pre>