2013-11-18 15:23:38 -06:00
|
|
|
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
extern mod extra;
|
2013-04-19 21:21:53 -05:00
|
|
|
|
2013-05-20 19:07:24 -05:00
|
|
|
use std::from_str::FromStr;
|
2013-11-18 15:23:38 -06:00
|
|
|
use std::num::One;
|
|
|
|
use std::num::Zero;
|
|
|
|
use std::num::FromPrimitive;
|
|
|
|
use extra::bigint::BigInt;
|
2013-04-18 17:53:09 -05:00
|
|
|
|
|
|
|
struct Context {
|
2013-11-18 15:23:38 -06:00
|
|
|
numer: BigInt,
|
|
|
|
accum: BigInt,
|
|
|
|
denom: BigInt,
|
2013-04-18 17:53:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Context {
|
|
|
|
fn new() -> Context {
|
2013-11-18 15:23:38 -06:00
|
|
|
Context {
|
|
|
|
numer: One::one(),
|
|
|
|
accum: Zero::zero(),
|
|
|
|
denom: One::one(),
|
2013-04-18 17:53:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-18 15:23:38 -06:00
|
|
|
fn from_int(i: int) -> BigInt {
|
|
|
|
FromPrimitive::from_int(i).unwrap()
|
2013-04-18 17:53:09 -05:00
|
|
|
}
|
|
|
|
|
2013-11-18 15:23:38 -06:00
|
|
|
fn extract_digit(&self) -> int {
|
|
|
|
if self.numer > self.accum {return -1;}
|
|
|
|
let (q, r) =
|
|
|
|
(self.numer * Context::from_int(3) + self.accum)
|
|
|
|
.div_rem(&self.denom);
|
|
|
|
if r + self.numer >= self.denom {return -1;}
|
|
|
|
q.to_int().unwrap()
|
|
|
|
}
|
2013-04-18 17:53:09 -05:00
|
|
|
|
2013-11-18 15:23:38 -06:00
|
|
|
fn next_term(&mut self, k: int) {
|
|
|
|
let y2 = Context::from_int(k * 2 + 1);
|
|
|
|
self.accum = (self.accum + (self.numer << 1)) * y2;
|
|
|
|
self.numer = self.numer * Context::from_int(k);
|
|
|
|
self.denom = self.denom * y2;
|
2013-04-18 17:53:09 -05:00
|
|
|
}
|
|
|
|
|
2013-11-18 15:23:38 -06:00
|
|
|
fn eliminate_digit(&mut self, d: int) {
|
|
|
|
let d = Context::from_int(d);
|
|
|
|
let ten = Context::from_int(10);
|
|
|
|
self.accum = (self.accum - self.denom * d) * ten;
|
|
|
|
self.numer = self.numer * ten;
|
2013-04-18 17:53:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-18 15:23:38 -06:00
|
|
|
fn pidigits(n: int) {
|
|
|
|
let mut k = 0;
|
|
|
|
let mut context = Context::new();
|
2013-04-18 17:53:09 -05:00
|
|
|
|
2013-11-18 15:23:38 -06:00
|
|
|
for i in range(1, n + 1) {
|
|
|
|
let mut d;
|
2013-04-18 17:53:09 -05:00
|
|
|
loop {
|
2013-11-18 15:23:38 -06:00
|
|
|
k += 1;
|
|
|
|
context.next_term(k);
|
|
|
|
d = context.extract_digit();
|
|
|
|
if d != -1 {break;}
|
|
|
|
}
|
2013-04-18 17:53:09 -05:00
|
|
|
|
2013-11-18 15:23:38 -06:00
|
|
|
print!("{}", d);
|
|
|
|
if i % 10 == 0 {print!("\t:{}\n", i);}
|
2013-04-18 17:53:09 -05:00
|
|
|
|
2013-11-18 15:23:38 -06:00
|
|
|
context.eliminate_digit(d);
|
|
|
|
}
|
2013-04-18 17:53:09 -05:00
|
|
|
|
2013-11-18 15:23:38 -06:00
|
|
|
let m = n % 10;
|
|
|
|
if m != 0 {
|
|
|
|
for _ in range(m, 10) {print(" ");}
|
|
|
|
print!("\t:{}\n", n);
|
2013-04-18 17:53:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2013-11-18 15:23:38 -06:00
|
|
|
let args = std::os::args();
|
|
|
|
let n = if args.len() < 2 {
|
|
|
|
512
|
|
|
|
} else {
|
|
|
|
FromStr::from_str(args[1]).unwrap()
|
|
|
|
};
|
2013-04-18 17:53:09 -05:00
|
|
|
pidigits(n);
|
|
|
|
}
|