11 releases
0.3.0 | Aug 30, 2021 |
---|---|
0.2.0 | Aug 28, 2021 |
0.1.9 | Aug 28, 2021 |
#1429 in Algorithms
36 downloads per month
Used in rotation
5KB
50 lines
gcd_bitwise
Disclaimer: The code is not mine.
The underlying code is part of the coreutils project. I have forked it for ease of use, for those who dont want to pull in big dependencies. I modified some parts for general use cases, eg. implementing generic types. This crate is dependency free.
Update!
numeric types of arguments will be cast to usize
instead of u32
from now.
You can pass u8
, u16
, u32
, u64
and usize
numeric types into gcd()
function. But please note that the 2 numbers that you pass must have the same type. Passing any signed type (like i32
) may give unexpected results. Please have a look at the Quick Start section below for examples.
Some Notes
This code uses stein's algorithm, that replaces division with arithmetic shifts, comparisons, and subtraction, for optimization of performance. For more info please refer to this page.
Quick Start
use gcd_bitwise::interface::gcd;
fn main() {
// For `u8` type
let num1: u8 = 15;
let num2: u8 = 51;
let gcd: u8 = gcd(num1, num2);
println!("gcd: {}", gcd); // 3
// For `usize` type
let num1: usize = 65535;
let num2: usize = 125;
let gcd: usize = gcd(num1, num2);
println!("gcd: {}", gcd); // 5
// And on it goes...
}
Final Note:
If you find any problems dont hesitate to open an issue on github.