Logo

Programming-Idioms

History of Idiom 74 > diff from v8 to v9

Edit summary for version 9 by :

Version 8

2015-08-25, 21:06:54

Version 9

2015-08-25, 21:17:11

Idiom #74 Compute GCD

Compute the greatest common divisor x of big integers a and b. Use an integer type able to handle huge numbers.

Idiom #74 Compute GCD

Compute the greatest common divisor x of big integers a and b. Use an integer type able to handle huge numbers.

Imports
import std.bigint;
Code
BigInt gcd(in BigInt x, in BigInt y) pure {
    if (y == 0)
        return x;
    return gcd(y, x%y);
}

gcd(a, b);
Comments bubble
As this time, std.numeric.gcd doesn't work with BigInts. Here is a non-optimal but working implementation.
Origin
http://rosettacode.org/wiki/Greatest_common_divisor#D