forked from TheAlgorithms/Go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spigotpi.go
59 lines (55 loc) · 1.26 KB
/
spigotpi.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
// spigotpi.go
// description: A Spigot Algorithm for the Digits of Pi
// details:
// implementation of Spigot Algorithm for the Digits of Pi - [Spigot algorithm](https://en.wikipedia.org/wiki/Spigot_algorithm)
// author(s) [red_byte](https://github.com/i-redbyte)
// see spigotpi_test.go
package pi
import "strconv"
func Spigot(n int) string {
pi := ""
boxes := n * 10 / 3
remainders := make([]int, boxes)
for i := 0; i < boxes; i++ {
remainders[i] = 2
}
digitsHeld := 0
for i := 0; i < n; i++ {
carriedOver := 0
sum := 0
for j := boxes - 1; j >= 0; j-- {
remainders[j] *= 10
sum = remainders[j] + carriedOver
quotient := sum / (j*2 + 1)
remainders[j] = sum % (j*2 + 1)
carriedOver = quotient * j
}
remainders[0] = sum % 10
q := sum / 10
switch q {
case 9:
digitsHeld++
case 10:
q = 0
for k := 1; k <= digitsHeld; k++ {
replaced, _ := strconv.Atoi(pi[i-k : i-k+1])
if replaced == 9 {
replaced = 0
} else {
replaced++
}
pi = delChar(pi, i-k)
pi = pi[:i-k] + strconv.Itoa(replaced) + pi[i-k:]
}
digitsHeld = 1
default:
digitsHeld = 1
}
pi += strconv.Itoa(q)
}
return pi
}
func delChar(s string, index int) string {
tmp := []rune(s)
return string(append(tmp[0:index], tmp[index+1:]...))
}