Debian Codegraph @

shootout/pidigits.go

raw symbols SCIP
1 //go:build ignore
2 // +build ignore
3
4 /*
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 * Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10
11 * Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14
15 * Neither the name of "The Computer Language Benchmarks Game" nor the
16 name of "The Computer Language Shootout Benchmarks" nor the names of
17 its contributors may be used to endorse or promote products derived
18 from this software without specific prior written permission.
19
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /* The Computer Language Benchmarks Game
34 * http://shootout.alioth.debian.org/
35 *
36 * contributed by The Go Authors.
37 * based on pidigits.c (by Paolo Bonzini & Sean Bartlett,
38 * modified by Michael Mellor)
39 */
40
41 package main
42
43 import (
44 "flag"
45 "fmt"
46 "math/big"
47 )
48
49 var n = flag.Int("n", 27, "number of digits")
50 var silent = flag.Bool("s", false, "don't print result")
51
52 var (
53 tmp1 = big.NewInt(0)
54 tmp2 = big.NewInt(0)
55 tmp3 = big.NewInt(0)
56 y2 = big.NewInt(0)
57 bigk = big.NewInt(0)
58 numer = big.NewInt(1)
59 accum = big.NewInt(0)
60 denom = big.NewInt(1)
61 ten = big.NewInt(10)
62 )
63
64 func extract_digit() int64 {
65 if numer.Cmp(accum) > 0 {
66 return -1
67 }
68
69 // Compute (numer * 3 + accum) / denom
70 tmp1.Lsh(numer, 1)
71 tmp1.Add(tmp1, numer)
72 tmp1.Add(tmp1, accum)
73 tmp1.DivMod(tmp1, denom, tmp2)
74
75 // Now, if (numer * 4 + accum) % denom...
76 tmp2.Add(tmp2, numer)
77
78 // ... is normalized, then the two divisions have the same result.
79 if tmp2.Cmp(denom) >= 0 {
80 return -1
81 }
82
83 return tmp1.Int64()
84 }
85
86 func next_term(k int64) {
87 y2.SetInt64(k*2 + 1)
88 bigk.SetInt64(k)
89
90 tmp1.Lsh(numer, 1)
91 accum.Add(accum, tmp1)
92 accum.Mul(accum, y2)
93 numer.Mul(numer, bigk)
94 denom.Mul(denom, y2)
95 }
96
97 func eliminate_digit(d int64) {
98 tmp3.SetInt64(d)
99 accum.Sub(accum, tmp3.Mul(denom, tmp3))
100 accum.Mul(accum, ten)
101 numer.Mul(numer, ten)
102 }
103
104 func printf(s string, arg ...interface{}) {
105 if !*silent {
106 fmt.Printf(s, arg...)
107 }
108 }
109
110 func main() {
111 flag.Parse()
112
113 var m int // 0 <= m < 10
114 for i, k := 0, int64(0); ; {
115 d := int64(-1)
116 for d < 0 {
117 k++
118 next_term(k)
119 d = extract_digit()
120 }
121
122 printf("%c", d+'0')
123
124 i++
125 m = i % 10
126 if m == 0 {
127 printf("\t:%d\n", i)
128 }
129 if i >= *n {
130 break
131 }
132 eliminate_digit(d)
133 }
134
135 if m > 0 {
136 printf("%s\t:%d\n", " "[m:10], *n)
137 }
138 }