Faulhaber's formula: Difference between revisions

→‎{{header|Sidef}}: switched to built-in polynomials
m (syntax highlighting fixup automation)
(→‎{{header|Sidef}}: switched to built-in polynomials)
Tag: Made through Tor
Line 2,824:
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">constfunc AnyNum = requirefaulhaber_formula('Math::AnyNum'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|
Poly.monomial(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 ⟶ 2,835:
{{out}}
<pre>
0: nx
1: 1/2 n*x^2 + 1/2 n*x
2: 1/3 n*x^3 + 1/2 n*x^2 + 1/6 n*x
3: 1/4 n*x^4 + 1/2 n*x^3 + 1/4 n*x^2
4: 1/5 n*x^5 + 1/2 n*x^4 + 1/3 n*x^3 - 1/30 n*x
5: 1/6 n*x^6 + 1/2 n*x^5 + 5/12 n*x^4 - 1/12 n*x^2
6: 1/7 n*x^7 + 1/2 n*x^6 + 1/2 n*x^5 - 1/6 n*x^3 + 1/42 n*x
7: 1/8 n*x^8 + 1/2 n*x^7 + 7/12 n*x^6 - 7/24 n*x^4 + 1/12 n*x^2
8: 1/9 n*x^9 + 1/2 n*x^8 + 2/3 n*x^7 - 7/15 n*x^5 + 2/9 n*x^3 - 1/30 n*x
9: 1/10 n*x^10 + 1/2 n*x^9 + 3/4 n*x^8 - 7/10 n*x^6 + 1/2 n*x^4 - 3/20 n*x^2
</pre>
 
2,756

edits