2014-09-16 18:40:47 +00:00
|
|
|
// The Computer Language Benchmarks Game
|
|
|
|
// http://benchmarksgame.alioth.debian.org/
|
|
|
|
//
|
|
|
|
// contributed by the Rust Project Developers
|
|
|
|
|
|
|
|
// Copyright (c) 2013-2014 The Rust Project Developers
|
|
|
|
//
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions
|
|
|
|
// are met:
|
|
|
|
//
|
|
|
|
// - Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
//
|
|
|
|
// - Redistributions in binary form must reproduce the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer in
|
|
|
|
// the documentation and/or other materials provided with the
|
|
|
|
// distribution.
|
|
|
|
//
|
|
|
|
// - Neither the name of "The Computer Language Benchmarks Game" nor
|
|
|
|
// the name of "The Computer Language Shootout Benchmarks" nor the
|
|
|
|
// names of its contributors may be used to endorse or promote
|
|
|
|
// products derived from this software without specific prior
|
|
|
|
// written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
|
|
|
// OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
2015-03-18 04:22:06 +00:00
|
|
|
#![feature(core)]
|
|
|
|
#![feature(test)]
|
|
|
|
|
2014-09-16 18:40:47 +00:00
|
|
|
extern crate num;
|
|
|
|
extern crate test;
|
|
|
|
|
2014-11-17 13:38:12 +00:00
|
|
|
use std::str::FromStr;
|
2015-01-03 18:30:05 +00:00
|
|
|
use std::num::{FromPrimitive, ToPrimitive};
|
2014-09-16 18:40:47 +00:00
|
|
|
|
|
|
|
use test::Bencher;
|
|
|
|
|
2014-11-15 03:30:48 +00:00
|
|
|
use num::{BigInt, Integer, One, Zero};
|
2014-09-16 18:40:47 +00:00
|
|
|
|
|
|
|
struct Context {
|
|
|
|
numer: BigInt,
|
|
|
|
accum: BigInt,
|
|
|
|
denom: BigInt,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Context {
|
|
|
|
fn new() -> Context {
|
|
|
|
Context {
|
|
|
|
numer: One::one(),
|
|
|
|
accum: Zero::zero(),
|
|
|
|
denom: One::one(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-12 04:51:02 +00:00
|
|
|
fn from_i32(i: i32) -> BigInt {
|
|
|
|
FromPrimitive::from_i32(i).unwrap()
|
2014-09-16 18:40:47 +00:00
|
|
|
}
|
|
|
|
|
2015-01-12 04:51:02 +00:00
|
|
|
fn extract_digit(&self) -> i32 {
|
2014-09-16 18:40:47 +00:00
|
|
|
if self.numer > self.accum {return -1;}
|
|
|
|
let (q, r) =
|
2015-01-12 04:51:02 +00:00
|
|
|
(&self.numer * Context::from_i32(3) + &self.accum)
|
2014-09-16 18:40:47 +00:00
|
|
|
.div_rem(&self.denom);
|
2014-12-16 14:38:35 +00:00
|
|
|
if r + &self.numer >= self.denom {return -1;}
|
2015-01-12 04:51:02 +00:00
|
|
|
q.to_i32().unwrap()
|
2014-09-16 18:40:47 +00:00
|
|
|
}
|
|
|
|
|
2015-01-12 04:51:02 +00:00
|
|
|
fn next_term(&mut self, k: i32) {
|
|
|
|
let y2 = Context::from_i32(k * 2 + 1);
|
2014-12-16 14:38:35 +00:00
|
|
|
self.accum = (&self.accum + (&self.numer << 1)) * &y2;
|
2015-01-12 04:51:02 +00:00
|
|
|
self.numer = &self.numer * Context::from_i32(k);
|
2014-12-16 14:38:35 +00:00
|
|
|
self.denom = &self.denom * y2;
|
2014-09-16 18:40:47 +00:00
|
|
|
}
|
|
|
|
|
2015-01-12 04:51:02 +00:00
|
|
|
fn eliminate_digit(&mut self, d: i32) {
|
|
|
|
let d = Context::from_i32(d);
|
|
|
|
let ten = Context::from_i32(10);
|
2014-12-16 14:38:35 +00:00
|
|
|
self.accum = (&self.accum - &self.denom * d) * &ten;
|
|
|
|
self.numer = &self.numer * ten;
|
2014-09-16 18:40:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-12 04:51:02 +00:00
|
|
|
fn pidigits(n: isize) {
|
2014-09-16 18:40:47 +00:00
|
|
|
let mut k = 0;
|
|
|
|
let mut context = Context::new();
|
|
|
|
|
2015-01-12 04:54:25 +00:00
|
|
|
for i in (1..(n+1)) {
|
2014-09-16 18:40:47 +00:00
|
|
|
let mut d;
|
|
|
|
loop {
|
|
|
|
k += 1;
|
|
|
|
context.next_term(k);
|
|
|
|
d = context.extract_digit();
|
|
|
|
if d != -1 {break;}
|
|
|
|
}
|
|
|
|
|
|
|
|
print!("{}", d);
|
|
|
|
if i % 10 == 0 {print!("\t:{}\n", i);}
|
|
|
|
|
|
|
|
context.eliminate_digit(d);
|
|
|
|
}
|
|
|
|
|
|
|
|
let m = n % 10;
|
|
|
|
if m != 0 {
|
2015-01-12 04:54:25 +00:00
|
|
|
for _ in (m..10) { print!(" "); }
|
2014-09-16 18:40:47 +00:00
|
|
|
print!("\t:{}\n", n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-12 04:51:02 +00:00
|
|
|
static DEFAULT_DIGITS: isize = 512;
|
2014-09-16 18:40:47 +00:00
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn use_bencher(b: &mut Bencher) {
|
|
|
|
b.iter(|| pidigits(DEFAULT_DIGITS))
|
|
|
|
}
|
|
|
|
|
2015-03-18 04:22:06 +00:00
|
|
|
#[allow(dead_code)]
|
2014-09-16 18:40:47 +00:00
|
|
|
fn main() {
|
2015-03-18 04:22:06 +00:00
|
|
|
let args = std::env::args().collect::<Vec<_>>();
|
2014-09-16 18:40:47 +00:00
|
|
|
let n = if args.len() < 2 {
|
|
|
|
DEFAULT_DIGITS
|
|
|
|
} else {
|
|
|
|
FromStr::from_str(args[1].as_slice()).unwrap()
|
|
|
|
};
|
|
|
|
pidigits(n);
|
|
|
|
}
|