Click here to Skip to main content
15,889,992 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.5K   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 
Hi,

1. Apparently I refer to "null" (re: "...to check if the result is null") as mathematical term, which is obvious from the code fragment (b!=0); this line is present in my original article and then repeated exactly in your code snippet, so I do not understand the source of confusion.
2. "Stack space" is not the only and by the way, not a primary optimization criteria in modern SW engineering.
3. Code branching is absolutely valid approach; in this particular case it helps significantly reduce the "number crunching" load on the system.
4. In average, my algorithm provide much better performance in a wide range of numbers than that "truncated" version you have described.
Thanks for your comments and have a great day.

A. Bell
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 

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.