Created
October 26, 2014 12:35
-
-
Save Buttonwood/021aa11ad468d5dd2367 to your computer and use it in GitHub Desktop.
A short python script for gcd implement!
This file contains hidden or 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
| def gcd_bruteforce(m,n): | |
| l = min(m,n) | |
| gcd = 0 | |
| for x in xrange(1,l): | |
| if m % x == 0 and n % x == 0: | |
| gcd = x | |
| return gcd | |
| def gcd_euclidean(m,n): | |
| l = n % m | |
| while l != 0: | |
| n = m | |
| m = l | |
| l = n % m | |
| return m | |
| def gcd_recursive(m,n): | |
| if m == 0: | |
| return n | |
| else: | |
| return gcd_recursive(n % m, m) | |
| def test(): | |
| print gcd_bruteforce(32,24) | |
| print gcd_bruteforce(24,32) | |
| print gcd_euclidean(32,24) | |
| print gcd_euclidean(24,32) | |
| print gcd_recursive(32,24) | |
| print gcd_recursive(24,32) | |
| if __name__ == '__main__': | |
| test() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment