A Big integer (signed version: BigInt
, unsigned version: BigUint
).
A BigUint
is represented as a vector of BigDigit
s.
A BigInt
is a combination of BigUint
and Sign
.
Common numerical operations are overloaded, so we can treat them
the same way we treat other numbers.
Example
extern crate num_bigint;
extern crate num_traits;
# fn main() {
use num_bigint::BigUint;
use num_traits::{Zero, One};
use std::mem::replace;
fn fib(n: usize) -> BigUint {
let mut f0: BigUint = Zero::zero();
let mut f1: BigUint = One::one();
for _ in 0..n {
let f2 = f0 + &f1;
f0 = replace(&mut f1, f2);
}
f0
}
println!("fib(1000) = {}", fib(1000));
# }
It's easy to generate large random numbers:
extern crate rand;
extern crate num_bigint as bigint;
# #[cfg(feature = "rand")]
# fn main() {
use bigint::{ToBigInt, RandBigInt};
let mut rng = rand::thread_rng();
let a = rng.gen_bigint(1000);
let low = -10000.to_bigint().unwrap();
let high = 10000.to_bigint().unwrap();
let b = rng.gen_bigint_range(&low, &high);
println!("{}", a * b);
# }
# #[cfg(not(feature = "rand"))]
# fn main() {
# }