Click here to Skip to main content
Click here to Skip to main content

Fast Greatest Common Divisor (GCD) Algorithm

By , 14 Feb 2011
 
       /// <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)

About the Author

jfriedman
Software Developer (Senior) ASTI Transportation Inc.
United States United States
Member
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...
 
v//

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralDear Julius (jfriedman): My original algorithm (the core pa...memberDrABELL17 Feb '11 - 12:17 
GeneralDr. Abell, Please stop. http://www.codeproject.com/KB/reci...memberjfriedman17 Feb '11 - 11:30 
GeneralHi Richard, Regarding my previous post: obviously I was tal...memberDrABELL17 Feb '11 - 2:59 
GeneralRe: "You point (1) is just a re-wording of my statement ..." No...memberRichard Deeming21 Feb '11 - 10:54 
General"some languages (e.g. VB) by default use ByRef instead of by...memberRichard Deeming17 Feb '11 - 2:33 
GeneralHi, 1. Apparently I refer to "null" (re: "...to check if th...memberDrABELL15 Feb '11 - 5:22 
GeneralDr.Abell, int is a value type which can never be null. The b...memberjfriedman15 Feb '11 - 4:03 
GeneralJulius, Thanks for your comment, but I still think that simp...memberDrABELL15 Feb '11 - 2:29 
General1.) You are claiming that if both numbers are equal the Eucl...memberjfriedman14 Feb '11 - 23:25 
General1. There are 3 reasons given: which one you are talking abou...memberDrABELL14 Feb '11 - 18:23 
GeneralI am not sure how correct your assertation is... I dont' use...memberjfriedman14 Feb '11 - 10:37 
GeneralHi: I would NOT recommend the Alternate 3 for several reaso...memberDrABELL14 Feb '11 - 9:16 

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 14 Feb 2011
Article Copyright 2011 by jfriedman
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid