blob: 1914cf214fe8e4a1e2453682ad767566cb8a6d82 [file] [log] [blame]
Russ Coxc3f43192012-03-06 23:27:30 -05001// +build ignore
2
Russ Coxcce01112009-09-30 11:51:08 -07003/*
4Redistribution and use in source and binary forms, with or without
5modification, are permitted provided that the following conditions are met:
6
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9
10 * Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13
14 * Neither the name of "The Computer Language Benchmarks Game" nor the
15 name of "The Computer Language Shootout Benchmarks" nor the names of
16 its contributors may be used to endorse or promote products derived
17 from this software without specific prior written permission.
18
19THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29POSSIBILITY OF SUCH DAMAGE.
30*/
31
32/* The Computer Language Benchmarks Game
33 * http://shootout.alioth.debian.org/
34 *
35 * contributed by The Go Authors.
36 * based on pidigits.c (by Paolo Bonzini & Sean Bartlett,
37 * modified by Michael Mellor)
38 */
39
40package main
41
42import (
Russ Coxc3f43192012-03-06 23:27:30 -050043 big "."
Robert Griesemer5a1d3322009-12-15 15:33:31 -080044 "fmt"
45 "runtime"
Russ Coxcce01112009-09-30 11:51:08 -070046)
47
48var (
Robert Griesemer5a1d3322009-12-15 15:33:31 -080049 tmp1 = big.NewInt(0)
50 tmp2 = big.NewInt(0)
51 numer = big.NewInt(1)
52 accum = big.NewInt(0)
53 denom = big.NewInt(1)
54 ten = big.NewInt(10)
Russ Coxcce01112009-09-30 11:51:08 -070055)
56
57func extractDigit() int64 {
58 if big.CmpInt(numer, accum) > 0 {
Robert Griesemer40621d52009-11-09 12:07:39 -080059 return -1
Russ Coxcce01112009-09-30 11:51:08 -070060 }
Robert Griesemer5a1d3322009-12-15 15:33:31 -080061 tmp1.Lsh(numer, 1).Add(tmp1, numer).Add(tmp1, accum)
62 big.DivModInt(tmp1, tmp2, tmp1, denom)
63 tmp2.Add(tmp2, numer)
Russ Coxcce01112009-09-30 11:51:08 -070064 if big.CmpInt(tmp2, denom) >= 0 {
Robert Griesemer40621d52009-11-09 12:07:39 -080065 return -1
Russ Coxcce01112009-09-30 11:51:08 -070066 }
Robert Griesemer5a1d3322009-12-15 15:33:31 -080067 return tmp1.Int64()
Russ Coxcce01112009-09-30 11:51:08 -070068}
69
70func nextTerm(k int64) {
Robert Griesemer5a1d3322009-12-15 15:33:31 -080071 y2 := k*2 + 1
72 accum.Add(accum, tmp1.Lsh(numer, 1))
73 accum.Mul(accum, tmp1.SetInt64(y2))
74 numer.Mul(numer, tmp1.SetInt64(k))
75 denom.Mul(denom, tmp1.SetInt64(y2))
Russ Coxcce01112009-09-30 11:51:08 -070076}
77
78func eliminateDigit(d int64) {
Robert Griesemer5a1d3322009-12-15 15:33:31 -080079 accum.Sub(accum, tmp1.Mul(denom, tmp1.SetInt64(d)))
80 accum.Mul(accum, ten)
81 numer.Mul(numer, ten)
Russ Coxcce01112009-09-30 11:51:08 -070082}
83
84func main() {
Robert Griesemer5a1d3322009-12-15 15:33:31 -080085 i := 0
86 k := int64(0)
Russ Coxcce01112009-09-30 11:51:08 -070087 for {
Robert Griesemer5a1d3322009-12-15 15:33:31 -080088 d := int64(-1)
Russ Coxcce01112009-09-30 11:51:08 -070089 for d < 0 {
Robert Griesemer5a1d3322009-12-15 15:33:31 -080090 k++
91 nextTerm(k)
92 d = extractDigit()
Russ Coxcce01112009-09-30 11:51:08 -070093 }
Robert Griesemer5a1d3322009-12-15 15:33:31 -080094 eliminateDigit(d)
95 fmt.Printf("%c", d+'0')
Russ Coxcce01112009-09-30 11:51:08 -070096
97 if i++; i%50 == 0 {
Robert Griesemer5a1d3322009-12-15 15:33:31 -080098 fmt.Printf("\n")
Russ Coxcce01112009-09-30 11:51:08 -070099 if i >= 1000 {
Robert Griesemer40621d52009-11-09 12:07:39 -0800100 break
Russ Coxcce01112009-09-30 11:51:08 -0700101 }
102 }
103 }
104
Shenghou Ma1abd8d82012-03-21 00:51:48 +0800105 fmt.Printf("\n%d calls; bit sizes: %d %d %d\n", runtime.NumCgoCall(), numer.Len(), accum.Len(), denom.Len())
Russ Coxcce01112009-09-30 11:51:08 -0700106}