Click here to Skip to main content
11,408,708 members (62,704 online)
Click here to Skip to main content
Alternative Tip/Trick

Fast Greatest Common Divisor (GCD) Algorithm

, 14 Feb 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
/// /// 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);


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


About the Author

Software Developer (Senior) ASTI Transportation Inc.
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...

Follow on   Twitter   Google+

Comments and Discussions

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

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150414.5 | Last Updated 14 Feb 2011
Article Copyright 2011 by jfriedman
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid