8
8
// option. This file may not be copied, modified, or distributed
9
9
// except according to those terms.
10
10
11
- /*!
12
-
13
- A Big integer (signed version: `BigInt`, unsigned version: `BigUint`).
14
-
15
- A `BigUint` is represented as an array of `BigDigit`s.
16
- A `BigInt` is a combination of `BigUint` and `Sign`.
17
- */
11
+ //! A Big integer (signed version: `BigInt`, unsigned version: `BigUint`).
12
+ //!
13
+ //! A `BigUint` is represented as an array of `BigDigit`s.
14
+ //! A `BigInt` is a combination of `BigUint` and `Sign`.
15
+ //!
16
+ //! Common numerical operations are overloaded, so we can treat them
17
+ //! the same way we treat other numbers.
18
+ //!
19
+ //! ## Example
20
+ //!
21
+ //! ```rust
22
+ //! use num::bigint::BigUint;
23
+ //! use std::num::{Zero, One};
24
+ //! use std::mem::replace;
25
+ //!
26
+ //! // Calculate large fibonacci numbers.
27
+ //! fn fib(n: uint) -> BigUint {
28
+ //! let mut f0: BigUint = Zero::zero();
29
+ //! let mut f1: BigUint = One::one();
30
+ //! for _ in range(0, n) {
31
+ //! let f2 = f0 + f1;
32
+ //! // This is a low cost way of swapping f0 with f1 and f1 with f2.
33
+ //! f0 = replace(&mut f1, f2);
34
+ //! }
35
+ //! f0
36
+ //! }
37
+ //!
38
+ //! // This is a very large number.
39
+ //! println!("fib(1000) = {}", fib(1000));
40
+ //! ```
41
+ //!
42
+ //! It's easy to generate large random numbers:
43
+ //!
44
+ //! ```rust
45
+ //! use num::bigint::{ToBigInt, RandBigInt};
46
+ //! use std::rand;
47
+ //!
48
+ //! let mut rng = rand::task_rng();
49
+ //! let a = rng.gen_bigint(1000u);
50
+ //!
51
+ //! let low = -10000i.to_bigint().unwrap();
52
+ //! let high = 10000i.to_bigint().unwrap();
53
+ //! let b = rng.gen_bigint_range(&low, &high);
54
+ //!
55
+ //! // Probably an even larger number.
56
+ //! println!("{}", a * b);
57
+ //! ```
18
58
19
59
use Integer ;
20
60
use rand:: Rng ;
@@ -28,15 +68,11 @@ use std::num::{Zero, One, ToStrRadix, FromStrRadix};
28
68
use std:: string:: String ;
29
69
use std:: { uint, i64, u64} ;
30
70
31
- /**
32
- A `BigDigit` is a `BigUint`'s composing element.
33
- */
71
+ /// A `BigDigit` is a `BigUint`'s composing element.
34
72
pub type BigDigit = u32 ;
35
73
36
- /**
37
- A `DoubleBigDigit` is the internal type used to do the computations. Its
38
- size is the double of the size of `BigDigit`.
39
- */
74
+ /// A `DoubleBigDigit` is the internal type used to do the computations. Its
75
+ /// size is the double of the size of `BigDigit`.
40
76
pub type DoubleBigDigit = u64 ;
41
77
42
78
pub static ZERO_BIG_DIGIT : BigDigit = 0 ;
@@ -70,12 +106,10 @@ pub mod BigDigit {
70
106
}
71
107
}
72
108
73
- /**
74
- A big unsigned integer type.
75
-
76
- A `BigUint`-typed value `BigUint { data: vec!(a, b, c) }` represents a number
77
- `(a + b * BigDigit::base + c * BigDigit::base^2)`.
78
- */
109
+ /// A big unsigned integer type.
110
+ ///
111
+ /// A `BigUint`-typed value `BigUint { data: vec!(a, b, c) }` represents a number
112
+ /// `(a + b * BigDigit::base + c * BigDigit::base^2)`.
79
113
#[ deriving( Clone ) ]
80
114
pub struct BigUint {
81
115
data : Vec < BigDigit >
@@ -460,11 +494,9 @@ impl Integer for BigUint {
460
494
}
461
495
}
462
496
463
- /**
464
- * Calculates the Greatest Common Divisor (GCD) of the number and `other`
465
- *
466
- * The result is always positive
467
- */
497
+ /// Calculates the Greatest Common Divisor (GCD) of the number and `other`.
498
+ ///
499
+ /// The result is always positive.
468
500
#[ inline]
469
501
fn gcd ( & self , other : & BigUint ) -> BigUint {
470
502
// Use Euclid's algorithm
@@ -478,17 +510,15 @@ impl Integer for BigUint {
478
510
return n;
479
511
}
480
512
481
- /**
482
- * Calculates the Lowest Common Multiple (LCM) of the number and `other`
483
- */
513
+ /// Calculates the Lowest Common Multiple (LCM) of the number and `other`.
484
514
#[ inline]
485
515
fn lcm ( & self , other : & BigUint ) -> BigUint { ( ( * self * * other) / self . gcd ( other) ) }
486
516
487
- /// Returns `true` if the number can be divided by `other` without leaving a remainder
517
+ /// Returns `true` if the number can be divided by `other` without leaving a remainder.
488
518
#[ inline]
489
519
fn divides ( & self , other : & BigUint ) -> bool { ( * self % * other) . is_zero ( ) }
490
520
491
- /// Returns `true` if the number is divisible by `2`
521
+ /// Returns `true` if the number is divisible by `2`.
492
522
#[ inline]
493
523
fn is_even ( & self ) -> bool {
494
524
// Considering only the last digit.
@@ -498,7 +528,7 @@ impl Integer for BigUint {
498
528
}
499
529
}
500
530
501
- /// Returns `true` if the number is not divisible by `2`
531
+ /// Returns `true` if the number is not divisible by `2`.
502
532
#[ inline]
503
533
fn is_odd ( & self ) -> bool { !self . is_even ( ) }
504
534
}
@@ -1068,33 +1098,29 @@ impl Integer for BigInt {
1068
1098
}
1069
1099
}
1070
1100
1071
- /**
1072
- * Calculates the Greatest Common Divisor (GCD) of the number and `other`
1073
- *
1074
- * The result is always positive
1075
- */
1101
+ /// Calculates the Greatest Common Divisor (GCD) of the number and `other`.
1102
+ ///
1103
+ /// The result is always positive.
1076
1104
#[ inline]
1077
1105
fn gcd ( & self , other : & BigInt ) -> BigInt {
1078
1106
BigInt :: from_biguint ( Plus , self . data . gcd ( & other. data ) )
1079
1107
}
1080
1108
1081
- /**
1082
- * Calculates the Lowest Common Multiple (LCM) of the number and `other`
1083
- */
1109
+ /// Calculates the Lowest Common Multiple (LCM) of the number and `other`.
1084
1110
#[ inline]
1085
1111
fn lcm ( & self , other : & BigInt ) -> BigInt {
1086
1112
BigInt :: from_biguint ( Plus , self . data . lcm ( & other. data ) )
1087
1113
}
1088
1114
1089
- /// Returns `true` if the number can be divided by `other` without leaving a remainder
1115
+ /// Returns `true` if the number can be divided by `other` without leaving a remainder.
1090
1116
#[ inline]
1091
1117
fn divides ( & self , other : & BigInt ) -> bool { self . data . divides ( & other. data ) }
1092
1118
1093
- /// Returns `true` if the number is divisible by `2`
1119
+ /// Returns `true` if the number is divisible by `2`.
1094
1120
#[ inline]
1095
1121
fn is_even ( & self ) -> bool { self . data . is_even ( ) }
1096
1122
1097
- /// Returns `true` if the number is not divisible by `2`
1123
+ /// Returns `true` if the number is not divisible by `2`.
1098
1124
#[ inline]
1099
1125
fn is_odd ( & self ) -> bool { self . data . is_odd ( ) }
1100
1126
}
0 commit comments