gcd_bitwise
Disclaimer: The code is not mine.
The 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 for calculating gcd.
Big Update!
You can pass any numeric type into gcd()
function. You can pass u8
, u16
, u32
, u64
and hell, even usize
. But please note that the 2 numbers that you pass must have the same type. 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 on how efficient this algorithm is, please refer to this page.
Quick Start
use gcd;