Skip to content

Commit a4bc425

Browse files
committed
modified: Math/faulhaber_s_formula_explicit.sf
1 parent 252246b commit a4bc425

File tree

1 file changed

+17
-21
lines changed

1 file changed

+17
-21
lines changed

Math/faulhaber_s_formula_explicit.sf

+17-21
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,8 @@
11
#!/usr/bin/ruby
22

3-
#
4-
## http://rosettacode.org/wiki/Faulhaber%27s_formula
5-
#
6-
7-
func bernoulli({.is_one}) { 1/2 }
8-
func bernoulli({.is_odd}) { 0/1 }
9-
10-
func bernoulli(n) {
11-
12-
var a = []
13-
for m in ^(n+1) {
14-
a[m] = 1/(m + 1)
15-
for j in (m^..0 + 1) {
16-
a[j-1] = j*(a[j-1] - a[j])
17-
}
18-
}
19-
20-
return a[0]
21-
}
3+
# See also:
4+
# https://en.wikipedia.org/wiki/Faulhaber%27s_formula
5+
# https://rosettacode.org/wiki/Faulhaber%27s_formula
226

237
func faulhaber_s_formula(p) {
248

@@ -37,6 +21,18 @@ func faulhaber_s_formula(p) {
3721
"1/#{p + 1} * (#{formula})"
3822
}
3923

40-
for p in ^10 {
41-
printf("%2d: %s\n", p, faulhaber_s_formula(p))
24+
10.times {|p|
25+
printf("F(%s) = %s\n", p, faulhaber_s_formula(p))
4226
}
27+
28+
__END__
29+
F(0) = 1/1 * (n)
30+
F(1) = 1/2 * (n^2 + n)
31+
F(2) = 1/3 * (n^3 + 3/2*n^2 + 1/2*n)
32+
F(3) = 1/4 * (n^4 + 2*n^3 + n^2)
33+
F(4) = 1/5 * (n^5 + 5/2*n^4 + 5/3*n^3 + -1/6*n)
34+
F(5) = 1/6 * (n^6 + 3*n^5 + 5/2*n^4 + -1/2*n^2)
35+
F(6) = 1/7 * (n^7 + 7/2*n^6 + 7/2*n^5 + -7/6*n^3 + 1/6*n)
36+
F(7) = 1/8 * (n^8 + 4*n^7 + 14/3*n^6 + -7/3*n^4 + 2/3*n^2)
37+
F(8) = 1/9 * (n^9 + 9/2*n^8 + 6*n^7 + -21/5*n^5 + 2*n^3 + -3/10*n)
38+
F(9) = 1/10 * (n^10 + 5*n^9 + 15/2*n^8 + -7*n^6 + 5*n^4 + -3/2*n^2)

0 commit comments

Comments
 (0)