shootout-pidigits.rs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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::from_str::FromStr;
  42. use std::num::One;
  43. use std::num::Zero;
  44. use std::num::FromPrimitive;
  45. use test::Bencher;
  46. use num::Integer;
  47. use num::bigint::BigInt;
  48. struct Context {
  49. numer: BigInt,
  50. accum: BigInt,
  51. denom: BigInt,
  52. }
  53. impl Context {
  54. fn new() -> Context {
  55. Context {
  56. numer: One::one(),
  57. accum: Zero::zero(),
  58. denom: One::one(),
  59. }
  60. }
  61. fn from_int(i: int) -> BigInt {
  62. FromPrimitive::from_int(i).unwrap()
  63. }
  64. fn extract_digit(&self) -> int {
  65. if self.numer > self.accum {return -1;}
  66. let (q, r) =
  67. (self.numer * Context::from_int(3) + self.accum)
  68. .div_rem(&self.denom);
  69. if r + self.numer >= self.denom {return -1;}
  70. q.to_int().unwrap()
  71. }
  72. fn next_term(&mut self, k: int) {
  73. let y2 = Context::from_int(k * 2 + 1);
  74. self.accum = (self.accum + (self.numer << 1)) * y2;
  75. self.numer = self.numer * Context::from_int(k);
  76. self.denom = self.denom * y2;
  77. }
  78. fn eliminate_digit(&mut self, d: int) {
  79. let d = Context::from_int(d);
  80. let ten = Context::from_int(10);
  81. self.accum = (self.accum - self.denom * d) * ten;
  82. self.numer = self.numer * ten;
  83. }
  84. }
  85. fn pidigits(n: int) {
  86. let mut k = 0;
  87. let mut context = Context::new();
  88. for i in range(1, n + 1) {
  89. let mut d;
  90. loop {
  91. k += 1;
  92. context.next_term(k);
  93. d = context.extract_digit();
  94. if d != -1 {break;}
  95. }
  96. print!("{}", d);
  97. if i % 10 == 0 {print!("\t:{}\n", i);}
  98. context.eliminate_digit(d);
  99. }
  100. let m = n % 10;
  101. if m != 0 {
  102. for _ in range(m, 10) { print!(" "); }
  103. print!("\t:{}\n", n);
  104. }
  105. }
  106. static DEFAULT_DIGITS: int = 512;
  107. #[bench]
  108. fn use_bencher(b: &mut Bencher) {
  109. b.iter(|| pidigits(DEFAULT_DIGITS))
  110. }
  111. fn main() {
  112. let args = std::os::args();
  113. let args = args.as_slice();
  114. let n = if args.len() < 2 {
  115. DEFAULT_DIGITS
  116. } else {
  117. FromStr::from_str(args[1].as_slice()).unwrap()
  118. };
  119. pidigits(n);
  120. }