Created
May 4, 2012 06:13
-
-
Save wanderrful/2592498 to your computer and use it in GitHub Desktop.
Euclidean Algorithm in C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=begin | |
I found this randomly and wanted to save it for future reference. | |
Let a, b denote the numerator and denomator, respectively. Let 'gcd' denote the Greatest Common Divisor of both a and b. | |
=end | |
if(a==0 || b==0) | |
return gcd = 1; | |
else | |
{ | |
r = (a%b); | |
while ( r < 0 || r > 0) | |
{ | |
a = b; | |
b = r; | |
r = (a%b); | |
} | |
gcd = b; | |
} | |
return gcd; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@wanderrful
The code above returns gcd as 1 if either a or b =0 right?
but please take a look at this:
source:
http://mfleck.cs.illinois.edu/building-blocks/version-1.0/number-theory.pdf
thank you.