...
Run Format

Source file test/bench/shootout/pidigits.go

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

View as plain text