gcd-bitwise

Crates.iogcd-bitwise
lib.rsgcd-bitwise
version0.3.0
sourcesrc
created_at2021-08-25 14:37:52.017704
updated_at2021-08-30 20:34:59.370655
descriptionThe binary Euclidean algorithm for computing gcd.
homepage
repositoryhttps://github.com/Inoshy/rust-book-helper/tree/master/problems/gcd-bitwise
max_upload_size
id442188
size6,164
Tausif (tausifcreates)

documentation

https://docs.rs/gcd-bitwise/0.1.7

README

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.

Commit count: 0

cargo fmt