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;
18 use num::bigint::BigInt;
35 fn from_int(i: int) -> BigInt {
36 FromPrimitive::from_int(i).unwrap()
39 fn extract_digit(&self) -> int {
40 if self.numer > self.accum {return -1;}
42 (self.numer * Context::from_int(3) + self.accum)
43 .div_rem(&self.denom);
44 if r + self.numer >= self.denom {return -1;}
48 fn next_term(&mut self, k: int) {
49 let y2 = Context::from_int(k * 2 + 1);
50 self.accum = (self.accum + (self.numer << 1)) * y2;
51 self.numer = self.numer * Context::from_int(k);
52 self.denom = self.denom * y2;
55 fn eliminate_digit(&mut self, d: int) {
56 let d = Context::from_int(d);
57 let ten = Context::from_int(10);
58 self.accum = (self.accum - self.denom * d) * ten;
59 self.numer = self.numer * ten;
65 let mut context = Context::new();
67 for i in range(1, n + 1) {
72 d = context.extract_digit();
77 if i % 10 == 0 {print!("\t:{}\n", i);}
79 context.eliminate_digit(d);
84 for _ in range(m, 10) { print!(" "); }
90 let args = std::os::args();
91 let args = args.as_slice();
92 let n = if args.len() < 2 {
95 FromStr::from_str(args[1]).unwrap()