shootout-pidigits.rs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // The Computer Language Benchmarks Game
  2. // http://benchmarksgame.alioth.debian.org/
  3. //
  4. // contributed by the Rust Project Developers
  5. // Copyright (c) 2013-2014 The Rust Project Developers
  6. //
  7. // All rights reserved.
  8. //
  9. // Redistribution and use in source and binary forms, with or without
  10. // modification, are permitted provided that the following conditions
  11. // are met:
  12. //
  13. // - Redistributions of source code must retain the above copyright
  14. // notice, this list of conditions and the following disclaimer.
  15. //
  16. // - Redistributions in binary form must reproduce the above copyright
  17. // notice, this list of conditions and the following disclaimer in
  18. // the documentation and/or other materials provided with the
  19. // distribution.
  20. //
  21. // - Neither the name of "The Computer Language Benchmarks Game" nor
  22. // the name of "The Computer Language Shootout Benchmarks" nor the
  23. // names of its contributors may be used to endorse or promote
  24. // products derived from this software without specific prior
  25. // written permission.
  26. //
  27. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  28. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  29. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  30. // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  31. // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  32. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  33. // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  34. // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  36. // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  37. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  38. // OF THE POSSIBILITY OF SUCH DAMAGE.
  39. extern crate num;
  40. extern crate test;
  41. use std::str::FromStr;
  42. use std::num::FromPrimitive;
  43. use test::Bencher;
  44. use num::{BigInt, Integer, One, Zero};
  45. struct Context {
  46. numer: BigInt,
  47. accum: BigInt,
  48. denom: BigInt,
  49. }
  50. impl Context {
  51. fn new() -> Context {
  52. Context {
  53. numer: One::one(),
  54. accum: Zero::zero(),
  55. denom: One::one(),
  56. }
  57. }
  58. fn from_int(i: int) -> BigInt {
  59. FromPrimitive::from_int(i).unwrap()
  60. }
  61. fn extract_digit(&self) -> int {
  62. if self.numer > self.accum {return -1;}
  63. let (q, r) =
  64. (&self.numer * Context::from_int(3) + &self.accum)
  65. .div_rem(&self.denom);
  66. if r + &self.numer >= self.denom {return -1;}
  67. q.to_int().unwrap()
  68. }
  69. fn next_term(&mut self, k: int) {
  70. let y2 = Context::from_int(k * 2 + 1);
  71. self.accum = (&self.accum + (&self.numer << 1)) * &y2;
  72. self.numer = &self.numer * Context::from_int(k);
  73. self.denom = &self.denom * y2;
  74. }
  75. fn eliminate_digit(&mut self, d: int) {
  76. let d = Context::from_int(d);
  77. let ten = Context::from_int(10);
  78. self.accum = (&self.accum - &self.denom * d) * &ten;
  79. self.numer = &self.numer * ten;
  80. }
  81. }
  82. fn pidigits(n: int) {
  83. let mut k = 0;
  84. let mut context = Context::new();
  85. for i in range(1, n + 1) {
  86. let mut d;
  87. loop {
  88. k += 1;
  89. context.next_term(k);
  90. d = context.extract_digit();
  91. if d != -1 {break;}
  92. }
  93. print!("{}", d);
  94. if i % 10 == 0 {print!("\t:{}\n", i);}
  95. context.eliminate_digit(d);
  96. }
  97. let m = n % 10;
  98. if m != 0 {
  99. for _ in range(m, 10) { print!(" "); }
  100. print!("\t:{}\n", n);
  101. }
  102. }
  103. static DEFAULT_DIGITS: int = 512;
  104. #[bench]
  105. fn use_bencher(b: &mut Bencher) {
  106. b.iter(|| pidigits(DEFAULT_DIGITS))
  107. }
  108. fn main() {
  109. let args = std::os::args();
  110. let args = args.as_slice();
  111. let n = if args.len() < 2 {
  112. DEFAULT_DIGITS
  113. } else {
  114. FromStr::from_str(args[1].as_slice()).unwrap()
  115. };
  116. pidigits(n);
  117. }