Created
November 24, 2009 05:28
-
-
Save fujiwara/241663 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
big "gmp"; | |
"flag"; | |
"fmt"; | |
"strconv"; | |
) | |
var n = 0 | |
var silent = false | |
var ( | |
tmp1 = big.NewInt(0); | |
tmp2 = big.NewInt(0); | |
numer = big.NewInt(1); | |
accum = big.NewInt(0); | |
denom = big.NewInt(1); | |
ten = big.NewInt(10); | |
) | |
func extractDigit() int64 { | |
if big.CmpInt(numer, accum) > 0 { | |
return -1 | |
} | |
tmp1.Lsh(numer, 1).Add(tmp1, numer).Add(tmp1, accum); | |
big.DivModInt(tmp1, tmp2, tmp1, denom); | |
tmp2.Add(tmp2, numer); | |
if big.CmpInt(tmp2, denom) >= 0 { | |
return -1 | |
} | |
return tmp1.Int64(); | |
} | |
func nextTerm(k int64) { | |
y2 := k*2 + 1; | |
accum.Add(accum, tmp1.Lsh(numer, 1)); | |
accum.Mul(accum, tmp1.SetInt64(y2)); | |
numer.Mul(numer, tmp1.SetInt64(k)); | |
denom.Mul(denom, tmp1.SetInt64(y2)); | |
} | |
func eliminateDigit(d int64) { | |
accum.Sub(accum, tmp1.Mul(denom, tmp1.SetInt64(d))); | |
accum.Mul(accum, ten); | |
numer.Mul(numer, ten); | |
} | |
func printf(s string, arg ...) { | |
if !silent { | |
fmt.Printf(s, arg); | |
} | |
} | |
func main() { | |
flag.Parse(); | |
if flag.NArg() > 0 { n,_ = strconv.Atoi( flag.Arg(0) ) } | |
var m int; // 0 <= m < 10 | |
for i, k := 0, int64(0); ; { | |
d := int64(-1); | |
for d < 0 { | |
k++; | |
nextTerm(k); | |
d = extractDigit(); | |
} | |
printf("%c", d + '0'); | |
i++; | |
m = i%10; | |
if m == 0 { | |
printf("\t:%d\n", i); | |
} | |
if i >= n { | |
break; | |
} | |
eliminateDigit(d); | |
} | |
if m > 0 { | |
printf("%s\t:%d\n", " "[m : 10], n); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment