-
Notifications
You must be signed in to change notification settings - Fork 4
/
sum_of_powers.go
151 lines (127 loc) · 2.28 KB
/
sum_of_powers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"flag"
"fmt"
"math/big"
"os"
)
func main() {
flag.Usage = usage
flag.Parse()
if flag.NArg() < 2 {
usage()
}
num := atoi(flag.Arg(0))
degree := atoi(flag.Arg(1))
fmt.Println(sum(num, degree))
fmt.Println(bsum(num, degree))
}
func usage() {
fmt.Fprintln(os.Stderr, "usage: sums_of_power num degree")
flag.PrintDefaults()
os.Exit(2)
}
func atoi(s string) *big.Int {
n := new(big.Int)
n.SetString(s, 10)
return n
}
func sum(num, degree *big.Int) *big.Int {
one := big.NewInt(1)
res := big.NewInt(0)
for i := big.NewInt(0); i.Cmp(num) <= 0; i.Add(i, one) {
n := new(big.Int)
n.Exp(i, degree, nil)
res.Add(res, n)
}
return res
}
// bernoulli summation formula
func bsum(num, degree *big.Int) *big.Int {
if degree.Sign() < 0 {
return nil
}
o := big.NewInt(1)
m := new(big.Int)
m.Set(degree)
m.Add(m, o)
g := big.NewRat(1, 1)
s := new(big.Rat)
k := -1
for i := big.NewInt(0); i.Cmp(degree) <= 0; i.Add(i, o) {
if k++; k > 4 {
k = 3
}
if k >= 3 && k&1 != 0 {
g.Neg(g)
continue
}
a := bernoulli(i)
x := new(big.Int)
y := new(big.Int)
x.Set(num)
y.Set(m)
y.Sub(y, i)
x.Exp(x, y, nil)
b := new(big.Rat)
b.SetInt(x)
c := binomial(m, i)
a.Mul(a, b)
a.Mul(a, c)
a.Mul(a, g)
s.Add(s, a)
g.Neg(g)
}
x := new(big.Rat)
x.SetInt(m)
x.Inv(x)
s.Mul(s, x)
return s.Num()
}
// https://en.wikipedia.org/wiki/Bernoulli_number#Explicit_definition
// computes B−n
func bernoulli(m *big.Int) *big.Rat {
o := big.NewInt(1)
r := big.NewRat(0, 1)
for k := big.NewInt(0); k.Cmp(m) <= 0; k.Add(k, o) {
for v := big.NewInt(0); v.Cmp(k) <= 0; v.Add(v, o) {
c := big.NewInt(-1)
c.Exp(c, v, nil)
c1 := new(big.Rat)
c1.SetInt(c)
c2 := binomial(k, v)
c.Exp(v, m, nil)
d := big.NewInt(1)
d.Add(k, o)
c3 := new(big.Rat)
c3.SetFrac(c, d)
c1.Mul(c1, c2)
c1.Mul(c1, c3)
r.Add(r, c1)
}
}
return r
}
func binomial(n, k *big.Int) *big.Rat {
x := fact(n)
y := fact(k)
v := new(big.Int)
v.Sub(n, k)
z := fact(v)
y.Mul(y, z)
x.Quo(x, y)
return x
}
func fact(n *big.Int) *big.Rat {
o := big.NewInt(1)
if n.Cmp(o) <= 0 {
return big.NewRat(1, 1)
}
v := big.NewInt(1)
for i := big.NewInt(2); i.Cmp(n) <= 0; i.Add(i, o) {
v.Mul(v, i)
}
p := big.NewRat(1, 1)
p.SetInt(v)
return p
}