wiki:Functions/Gcd

gcd

Greatest Common Divisor

(define (gcd a b)
    (if (zero? b)
        a
        (gcd b (modulo a b))))
fun gcd (0, n) = n
  | gcd (m, n) = gcd (n mod m, m)
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a
from math import gcd
fn gcd(mut n: u64, mut m: u64) -> u64 {
    assert!(n != 0 && m != 0);
    while m != 0 {
        if m < n {
            let t = m;
            m = n;
            n = t;
        }
        m = m % n
    }
    n
}
package main

func gcd(x, y int) int {
    for y != 0 {
        x, y = y, x%y
    }
    return x
}
Last modified 16 months ago Last modified on Oct 24, 2018, 10:20:52 AM
Note: See TracWiki for help on using the wiki.