Faulhaber's formula: Difference between revisions
Content deleted Content added
Thundergnat (talk | contribs) 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"> |
<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 |
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: |
0: x |
||
1: 1/2 |
1: 1/2*x^2 + 1/2*x |
||
2: 1/3 |
2: 1/3*x^3 + 1/2*x^2 + 1/6*x |
||
3: 1/4 |
3: 1/4*x^4 + 1/2*x^3 + 1/4*x^2 |
||
4: 1/5 |
4: 1/5*x^5 + 1/2*x^4 + 1/3*x^3 - 1/30*x |
||
5: 1/6 |
5: 1/6*x^6 + 1/2*x^5 + 5/12*x^4 - 1/12*x^2 |
||
6: 1/7 |
6: 1/7*x^7 + 1/2*x^6 + 1/2*x^5 - 1/6*x^3 + 1/42*x |
||
7: 1/8 |
7: 1/8*x^8 + 1/2*x^7 + 7/12*x^6 - 7/24*x^4 + 1/12*x^2 |
||
8: 1/9 |
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 |
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> |
||