-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfibcalc.go
191 lines (160 loc) · 4.72 KB
/
fibcalc.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Package fibcalc implements the calculation of
// the Fibonacci number by raising the matrix to
// a power optimized enough to calculate large
// Fibonacci numbers.
package fibcalc
import (
"math/big"
"sync"
)
// Uint64 the best way to calculate Fibonacci
// numbers for small n and understand how to
// calculate a number by quickly raising the
// matrix to a power. Function takes an natural
// number [0,93] (uint64 limit).
func Uint64(n uint8) uint64 {
if n < 2 {
return uint64(n)
}
// It is based on the identity:
// | 1 1 |^n = | Fn+1 Fn |
// | 1 0 | | Fn Fn-1 |
// Temporary matrix for exponentiation
// A = | a b |
// | c d |
var a, b, c, d uint64 = 1, 1, 1, 0
// Results vector
// R = | rc rd |
var rc, rd uint64 = 0, 1
for n != 1 {
// If the n is odd
if n&1 != 0 {
// Multiply the vector R by the matrix A
rc, rd = rc*a+rd*c, rc*b+rd*d
}
// Multiply the matrix A by itself
a, b, c, d = a*a+b*c, a*b+b*d, c*a+d*c, c*b+d*d
// Halve the n
n >>= 1
}
return rc*a + rd*c
}
// Sequential quite effective way to calculate Fibonacci
// numbers for natural n large 128, using sequential
// calculations, but to calculate really large numbers
// it’s more efficient to use Concurrent function. See
// benchmarks for details.
func Sequential(n uint64) *big.Int {
// Optimization for numbers that can be obtained within uint64
if n < 94 {
return uint64calc(n)
}
var (
// Temporary matrix for exponentiation
// A = | a b | = | 1 1 |
// | c d | | 1 0 |
a, b = big.NewInt(1), big.NewInt(1)
c, d = big.NewInt(1), big.NewInt(0)
// Results vector
// R = | rc rd | = | 0 1 |
rc, rd = big.NewInt(0), big.NewInt(1)
// Temporary variables for calculations
// Fewer number of temporary variables can be dispensed with,
// but for n > 128, calculations thus become more efficient
tempA, tempB = &big.Int{}, &big.Int{}
copyA, copyB, copyC = &big.Int{}, &big.Int{}, &big.Int{}
)
for n != 1 {
// If the n is odd
if n&1 != 0 {
// Temporary copy for calculations
copyC.Set(rc)
// Multiply the vector R by the matrix A
// rc, rd = rc*a+rd*c, rc*b+rd*d
rc.Add(tempA.Mul(rc, a), tempB.Mul(rd, c))
rd.Add(tempB.Mul(copyC, b), tempA.Mul(rd, d))
}
// Temporary copy for calculations
copyA.Set(a)
copyB.Set(b)
copyC.Set(c)
// a, b, c, d = a*a+b*c, a*b+b*d, c*a+d*c, c*b+d*d
a.Add(tempA.Mul(copyA, a), tempB.Mul(copyB, copyC))
b.Add(tempA.Mul(copyA, copyB), tempB.Mul(copyB, d))
c.Add(tempA.Mul(copyC, copyA), tempB.Mul(d, copyC))
// This will save a few allocations for n > 2048
copyA.Set(d)
d.Add(tempA.Mul(copyC, copyB), tempB.Mul(d, copyA))
// Halve the n
n >>= 1
}
// rc*a+rd*c
return rc.Add(tempA.Mul(rc, a), tempB.Mul(rd, c))
}
// Concurrent is the best way to calculate very large
// Fibonacci numbers using concurrent computing. See
// benchmarks for details.
func Concurrent(n uint64) *big.Int {
// Optimization for numbers that can be obtained within uint64
if n < 94 {
return uint64calc(n)
}
var (
// Temporary matrix for exponentiation
// A = | a b | = | 1 1 |
// | c d | | 1 0 |
a, b = big.NewInt(1), big.NewInt(1)
c, d = big.NewInt(1), big.NewInt(0)
// Results vector
// R = | rc rd | = | 0 1 |
rc, rd = big.NewInt(0), big.NewInt(1)
// Temporary variables for calculations
aa, ab, bd = &big.Int{}, &big.Int{}, &big.Int{}
ca, cb = &big.Int{}, &big.Int{}
dc, dd = &big.Int{}, &big.Int{}
wg sync.WaitGroup
)
for n != 1 {
// If the n is odd
if n&1 != 0 {
// Preliminary calculations
wg.Add(4)
go func() { ca.Mul(rc, a); wg.Done() }()
go func() { dc.Mul(rd, c); wg.Done() }()
go func() { cb.Mul(rc, b); wg.Done() }()
go func() { dd.Mul(rd, d); wg.Done() }()
wg.Wait()
// Multiply the vector R by the matrix A
// rc, rd = rc*a+rd*c, rc*b+rd*d
wg.Add(2)
go func() { rc.Add(ca, dc); wg.Done() }()
go func() { rd.Add(cb, dd); wg.Done() }()
wg.Wait()
}
// Preliminary calculations
wg.Add(7)
go func() { aa.Mul(a, a); wg.Done() }()
go func() { cb.Mul(b, c); wg.Done() }()
go func() { ab.Mul(a, b); wg.Done() }()
go func() { bd.Mul(b, d); wg.Done() }()
go func() { ca.Mul(c, a); wg.Done() }()
go func() { dc.Mul(d, c); wg.Done() }()
go func() { dd.Mul(d, d); wg.Done() }()
wg.Wait()
// a, b, c, d = a*a+b*c, a*b+b*d, c*a+d*c, c*b+d*d
wg.Add(4)
go func() { a.Add(aa, cb); wg.Done() }()
go func() { b.Add(ab, bd); wg.Done() }()
go func() { c.Add(ca, dc); wg.Done() }()
go func() { d.Add(cb, dd); wg.Done() }()
wg.Wait()
// Halve the n
n >>= 1
}
//rc*a+rd*c
return d.Add(ca.Mul(rc, a), dc.Mul(rd, c))
}
// uint64calc big.Int wrapper for the Uint64 function
func uint64calc(n uint64) *big.Int {
return new(big.Int).SetUint64(Uint64(uint8(n)))
}