Robert Griesemer | 081bc69 | 2009-08-06 18:16:51 -0700 | [diff] [blame] | 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 ( |
Evan Shaw | 76cbbc8 | 2010-04-20 20:39:36 -0700 | [diff] [blame] | 41 | "big" |
Robert Griesemer | 45ca9f7 | 2009-12-15 15:41:46 -0800 | [diff] [blame] | 42 | "flag" |
| 43 | "fmt" |
Robert Griesemer | 081bc69 | 2009-08-06 18:16:51 -0700 | [diff] [blame] | 44 | ) |
| 45 | |
Russ Cox | 19dae07 | 2009-11-20 13:11:42 -0800 | [diff] [blame] | 46 | var n = flag.Int("n", 27, "number of digits") |
| 47 | var silent = flag.Bool("s", false, "don't print result") |
Robert Griesemer | 081bc69 | 2009-08-06 18:16:51 -0700 | [diff] [blame] | 48 | |
| 49 | var ( |
Evan Shaw | 76cbbc8 | 2010-04-20 20:39:36 -0700 | [diff] [blame] | 50 | tmp1 = big.NewInt(0) |
| 51 | tmp2 = big.NewInt(0) |
Evan Shaw | 5bf420f | 2010-10-30 20:16:44 -0700 | [diff] [blame] | 52 | tmp3 = big.NewInt(0) |
Evan Shaw | 76cbbc8 | 2010-04-20 20:39:36 -0700 | [diff] [blame] | 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) |
Robert Griesemer | 081bc69 | 2009-08-06 18:16:51 -0700 | [diff] [blame] | 59 | ) |
| 60 | |
| 61 | func extract_digit() int64 { |
| 62 | if numer.Cmp(accum) > 0 { |
Russ Cox | 19dae07 | 2009-11-20 13:11:42 -0800 | [diff] [blame] | 63 | return -1 |
Robert Griesemer | 081bc69 | 2009-08-06 18:16:51 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Robert Griesemer | 0dbd897 | 2009-08-10 17:44:46 -0700 | [diff] [blame] | 66 | // Compute (numer * 3 + accum) / denom |
Robert Griesemer | 58e7799 | 2010-04-30 21:25:48 -0700 | [diff] [blame] | 67 | tmp1.Lsh(numer, 1) |
Evan Shaw | 76cbbc8 | 2010-04-20 20:39:36 -0700 | [diff] [blame] | 68 | tmp1.Add(tmp1, numer) |
| 69 | tmp1.Add(tmp1, accum) |
| 70 | tmp1.DivMod(tmp1, denom, tmp2) |
Robert Griesemer | 081bc69 | 2009-08-06 18:16:51 -0700 | [diff] [blame] | 71 | |
Robert Griesemer | 0dbd897 | 2009-08-10 17:44:46 -0700 | [diff] [blame] | 72 | // Now, if (numer * 4 + accum) % denom... |
Evan Shaw | 76cbbc8 | 2010-04-20 20:39:36 -0700 | [diff] [blame] | 73 | tmp2.Add(tmp2, numer) |
Robert Griesemer | 081bc69 | 2009-08-06 18:16:51 -0700 | [diff] [blame] | 74 | |
Robert Griesemer | 0dbd897 | 2009-08-10 17:44:46 -0700 | [diff] [blame] | 75 | // ... is normalized, then the two divisions have the same result. |
Robert Griesemer | 081bc69 | 2009-08-06 18:16:51 -0700 | [diff] [blame] | 76 | if tmp2.Cmp(denom) >= 0 { |
Russ Cox | 19dae07 | 2009-11-20 13:11:42 -0800 | [diff] [blame] | 77 | return -1 |
Robert Griesemer | 081bc69 | 2009-08-06 18:16:51 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Evan Shaw | 76cbbc8 | 2010-04-20 20:39:36 -0700 | [diff] [blame] | 80 | return tmp1.Int64() |
Robert Griesemer | 081bc69 | 2009-08-06 18:16:51 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | func next_term(k int64) { |
Robert Griesemer | b9caa4a | 2010-05-03 18:48:05 -0700 | [diff] [blame] | 84 | y2.SetInt64(k*2 + 1) |
| 85 | bigk.SetInt64(k) |
Robert Griesemer | 081bc69 | 2009-08-06 18:16:51 -0700 | [diff] [blame] | 86 | |
Robert Griesemer | 58e7799 | 2010-04-30 21:25:48 -0700 | [diff] [blame] | 87 | tmp1.Lsh(numer, 1) |
Evan Shaw | 76cbbc8 | 2010-04-20 20:39:36 -0700 | [diff] [blame] | 88 | accum.Add(accum, tmp1) |
| 89 | accum.Mul(accum, y2) |
| 90 | numer.Mul(numer, bigk) |
| 91 | denom.Mul(denom, y2) |
Robert Griesemer | 081bc69 | 2009-08-06 18:16:51 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | func eliminate_digit(d int64) { |
Evan Shaw | 5bf420f | 2010-10-30 20:16:44 -0700 | [diff] [blame] | 95 | tmp3.SetInt64(d) |
| 96 | accum.Sub(accum, tmp3.Mul(denom, tmp3)) |
Evan Shaw | 76cbbc8 | 2010-04-20 20:39:36 -0700 | [diff] [blame] | 97 | accum.Mul(accum, ten) |
| 98 | numer.Mul(numer, ten) |
Robert Griesemer | 081bc69 | 2009-08-06 18:16:51 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Rob Pike | d2fc5d6 | 2010-02-02 10:53:37 +1100 | [diff] [blame] | 101 | func printf(s string, arg ...interface{}) { |
Robert Griesemer | 081bc69 | 2009-08-06 18:16:51 -0700 | [diff] [blame] | 102 | if !*silent { |
Russ Cox | 2ee420f | 2010-09-24 11:55:48 -0400 | [diff] [blame] | 103 | fmt.Printf(s, arg...) |
Robert Griesemer | 081bc69 | 2009-08-06 18:16:51 -0700 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | |
| 107 | func main() { |
Robert Griesemer | 45ca9f7 | 2009-12-15 15:41:46 -0800 | [diff] [blame] | 108 | flag.Parse() |
Robert Griesemer | 081bc69 | 2009-08-06 18:16:51 -0700 | [diff] [blame] | 109 | |
Robert Griesemer | 45ca9f7 | 2009-12-15 15:41:46 -0800 | [diff] [blame] | 110 | var m int // 0 <= m < 10 |
Robert Griesemer | 081bc69 | 2009-08-06 18:16:51 -0700 | [diff] [blame] | 111 | for i, k := 0, int64(0); ; { |
Robert Griesemer | 45ca9f7 | 2009-12-15 15:41:46 -0800 | [diff] [blame] | 112 | d := int64(-1) |
Robert Griesemer | 081bc69 | 2009-08-06 18:16:51 -0700 | [diff] [blame] | 113 | for d < 0 { |
Robert Griesemer | 45ca9f7 | 2009-12-15 15:41:46 -0800 | [diff] [blame] | 114 | k++ |
| 115 | next_term(k) |
| 116 | d = extract_digit() |
Robert Griesemer | 081bc69 | 2009-08-06 18:16:51 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Robert Griesemer | 45ca9f7 | 2009-12-15 15:41:46 -0800 | [diff] [blame] | 119 | printf("%c", d+'0') |
Robert Griesemer | 081bc69 | 2009-08-06 18:16:51 -0700 | [diff] [blame] | 120 | |
Robert Griesemer | 45ca9f7 | 2009-12-15 15:41:46 -0800 | [diff] [blame] | 121 | i++ |
| 122 | m = i % 10 |
Robert Griesemer | 081bc69 | 2009-08-06 18:16:51 -0700 | [diff] [blame] | 123 | if m == 0 { |
Russ Cox | 19dae07 | 2009-11-20 13:11:42 -0800 | [diff] [blame] | 124 | printf("\t:%d\n", i) |
Robert Griesemer | 081bc69 | 2009-08-06 18:16:51 -0700 | [diff] [blame] | 125 | } |
| 126 | if i >= *n { |
Russ Cox | 19dae07 | 2009-11-20 13:11:42 -0800 | [diff] [blame] | 127 | break |
Robert Griesemer | 081bc69 | 2009-08-06 18:16:51 -0700 | [diff] [blame] | 128 | } |
Robert Griesemer | 45ca9f7 | 2009-12-15 15:41:46 -0800 | [diff] [blame] | 129 | eliminate_digit(d) |
Robert Griesemer | 081bc69 | 2009-08-06 18:16:51 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | if m > 0 { |
Russ Cox | 19dae07 | 2009-11-20 13:11:42 -0800 | [diff] [blame] | 133 | printf("%s\t:%d\n", " "[m:10], *n) |
Robert Griesemer | 081bc69 | 2009-08-06 18:16:51 -0700 | [diff] [blame] | 134 | } |
| 135 | } |