| Crates.io | math-comb |
| lib.rs | math-comb |
| version | 0.2.3 |
| created_at | 2025-01-10 14:52:22.126862+00 |
| updated_at | 2025-01-14 16:12:37.787792+00 |
| description | Math library for Combinatorics, Modular arithmetic & Number Theory utilities. |
| homepage | https://github.com/Ashwin-1709/math-comb |
| repository | https://github.com/Ashwin-1709/math-comb |
| max_upload_size | |
| id | 1511333 |
| size | 24,255 |
Math library for Combinatorics, Modular arithmetic & Prime Factorization.
This library provides a collection of mathematical utilities:
Pollard-Rho algorithm)Add this crate to your Cargo.toml:
[dependencies]
math-comb = "0.2.3"
use math_comb::Comb;
fn main() {
let comb = Comb::new(
/* modulus = */ 1000000007,
/* max_fact = */ 5
);
println!("5 choose 2: {}", comb.nCr(5, 2)); // Output: 10
println!("5 permute 2: {}", comb.nPr(5, 2)); // Output: 60
}
use math_comb::Modexp;
fn main() {
let base: u64 = 2;
let exponent: u64 = 10;
let modulus: u64 = 1000000007;
println!("2^10 % 1000000007: {}", Modexp::mod_exp(base, exponent, modulus)); // Output: 1024
let x: u64 = 3;
let modulus: u64 = 11;
println!("Modular inverse of 3 mod 11: {}", Modexp::mod_inv(x, modulus)); // Output: 4
}
use math_comb::Prime;
fn main() {
let n: u64 = 15006435;
println!("A non-trivial factor of {}: {}", n, Prime::pollard(n)); // Output: A non-trivial factor of 15006435: 3 or 5 or 1000429
let factors = Prime::factor(n);
println!("Prime factors of {}: {:?}", n, factors); // Output: Prime factors of 15006435: [3, 5, 1000429]
let a: u64 = 1000000007;
println!("Is {} prime? {}", a, Prime::is_prime(a)); // Output: Is 1000000007 prime? true
let b: u64 = 21;
println!("Is {} prime? {}", b, Prime::is_prime(b)); // Output: Is 21 prime? false
}
use math_comb::Spf;
fn main() {
let spf = Spf::new(
/*max_limit = */ 10000000
);
// Get the smallest prime factor of a number
let number: u64 = 81;
println!("Smallest prime factor of {}: {}", number, spf.get_spf(number)); // Output: Smallest prime factor of 81: 3
// Factorize a number into its prime factors. This is O(logn) since we have precomputed spfs.
let number: u64 = 45;
let factors = spf.factorize(number);
println!("Prime factors of {}: {:?}", number, factors); // Output: Prime factors of 45: [3, 3, 5]
}
This library is licensed under MIT License.