Function goose::util::gcd

source ·
pub fn gcd(u: usize, v: usize) -> usize
Expand description

Calculate the greatest common divisor of two integers using binary GCD (or Stein’s) algorithm.

More detail on Wikipedia.

Example

use goose::util;

// 1 and any other integer are only divisible by 1.
assert_eq!(util::gcd(1, 100), 1);

// 9 and 103 are both divisible by 3.
assert_eq!(util::gcd(9, 102), 3);

// 12345 and 67890 are both divisible by 15.
assert_eq!(util::gcd(12345, 67890), 15);

// 2 and 5 are both divisible by 1.
assert_eq!(util::gcd(2, 5), 1);