1 // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
13 use std::from_str::FromStr;
16 use std::num::FromPrimitive;
17 use num::bigint::BigInt;
34 fn from_int(i: int) -> BigInt {
35 FromPrimitive::from_int(i).unwrap()
38 fn extract_digit(&self) -> int {
39 if self.numer > self.accum {return -1;}
41 (self.numer * Context::from_int(3) + self.accum)
42 .div_rem(&self.denom);
43 if r + self.numer >= self.denom {return -1;}
47 fn next_term(&mut self, k: int) {
48 let y2 = Context::from_int(k * 2 + 1);
49 self.accum = (self.accum + (self.numer << 1)) * y2;
50 self.numer = self.numer * Context::from_int(k);
51 self.denom = self.denom * y2;
54 fn eliminate_digit(&mut self, d: int) {
55 let d = Context::from_int(d);
56 let ten = Context::from_int(10);
57 self.accum = (self.accum - self.denom * d) * ten;
58 self.numer = self.numer * ten;
64 let mut context = Context::new();
66 for i in range(1, n + 1) {
71 d = context.extract_digit();
76 if i % 10 == 0 {print!("\t:{}\n", i);}
78 context.eliminate_digit(d);
83 for _ in range(m, 10) { print!(" "); }
89 let args = std::os::args();
90 let n = if args.len() < 2 {
93 FromStr::from_str(args[1]).unwrap()