Click here to Skip to main content
15,900,418 members
Articles / General Programming / Algorithms
Alternative
Tip/Trick

Fast Greatest Common Divisor (GCD) Algorithm

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
14 Feb 2011CPOL 32.7K   3   12
/// /// Find the Greatest Common Divisor /// /// Number a /// Number b /// The greatest common Divisor public static long GCD(long a, long b) ...
/// <summary>
/// Find the Greatest Common Divisor
/// </summary>
/// <param name="a">Number a</param>
/// <param name="b">Number b</param>
/// <returns>The greatest common Divisor</returns>
public static long GCD(long a, long b)
{
    while (b != 0)
    {
        long tmp = b;
        b = a % b;
        a = tmp;
    }

    return a;
}

/// <summary>
/// Find the Least Common Multiple
/// </summary>
/// <param name="a">Number a</param>
/// <param name="b">Number b</param>
/// <returns>The least common multiple</returns>
public static long LCM(long a, long b)
{
    return (a * b) / GCD(a,b);
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
Livin in a lonely world, caught the midnight train going anywhere... Only thing is it was a runaway train... and it ain't ever goin back...
мала ка на хари, Trahentes ex exsilium

Comments and Discussions

 
GeneralDear Julius (jfriedman): My original algorithm (the core pa... Pin
DrABELL17-Feb-11 12:17
DrABELL17-Feb-11 12:17 
GeneralDr. Abell, Please stop. http://www.codeproject.com/KB/reci... Pin
jfriedman17-Feb-11 11:30
jfriedman17-Feb-11 11:30 
GeneralHi Richard, Regarding my previous post: obviously I was tal... Pin
DrABELL17-Feb-11 2:59
DrABELL17-Feb-11 2:59 
GeneralRe: "You point (1) is just a re-wording of my statement ..." No... Pin
Richard Deeming21-Feb-11 10:54
mveRichard Deeming21-Feb-11 10:54 
General"some languages (e.g. VB) by default use ByRef instead of by... Pin
Richard Deeming17-Feb-11 2:33
mveRichard Deeming17-Feb-11 2:33 
GeneralHi, 1. Apparently I refer to "null" (re: "...to check if th... Pin
DrABELL15-Feb-11 5:22
DrABELL15-Feb-11 5:22 
GeneralDr.Abell, int is a value type which can never be null. The b... Pin
jfriedman15-Feb-11 4:03
jfriedman15-Feb-11 4:03 
GeneralJulius, Thanks for your comment, but I still think that simp... Pin
DrABELL15-Feb-11 2:29
DrABELL15-Feb-11 2:29 
General1.) You are claiming that if both numbers are equal the Eucl... Pin
jfriedman14-Feb-11 23:25
jfriedman14-Feb-11 23:25 
General1. There are 3 reasons given: which one you are talking abou... Pin
DrABELL14-Feb-11 18:23
DrABELL14-Feb-11 18:23 
GeneralI am not sure how correct your assertation is... I dont' use... Pin
jfriedman14-Feb-11 10:37
jfriedman14-Feb-11 10:37 
GeneralHi: I would NOT recommend the Alternate 3 for several reaso... Pin
DrABELL14-Feb-11 9:16
DrABELL14-Feb-11 9:16 
Hi:

I would NOT recommend the Alternate 3 for several reason:

1.GCD calculation in Alternate 3 is just a truncated version of the ORIGINAL one (using the same Euclid algorithm, slightly re-written in terms of vars naming), and it's lacking the safety/performance boost provided in ORIGINAL one by the thorough validating/checking of initial conditions: for example, there is no need to run the Euclid part if both numbers are the same, or if the smaller number is GCD, etc.).
2. Alternate 1 is less portable and less safe because it could create a mess with parameters a and b: some languages (e.g. VB) by default use ByRef instead of byBal, so the values a and b could get corrupted in some programming languages.
3. Least Common Multiple (LCM), also known as LCD (Least Common Denominator) calculation shown in Alternate 3 is correct; it is based on the GCD computation and a trivial relationship like: LCD = value1 * value2 / GCD (value1, value2), which is actually implemented in a fraction calculator. I am going to publish the full-size article on several core algorithms, pertinent to the Fraction Calculator and Prime Factoring, covering all these topics.

Thanks and regards,
Alex

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.