Skip to content

Instantly share code, notes, and snippets.

@Walkeryr
Last active September 3, 2017 12:06
Show Gist options
  • Save Walkeryr/292750fc262c05e38e2d5b2c246374fb to your computer and use it in GitHub Desktop.
Save Walkeryr/292750fc262c05e38e2d5b2c246374fb to your computer and use it in GitHub Desktop.
TAOCP — 1.1.1E: Euclid‘s Algorithm

Book: The Art of Computer Programming, Volume 1: Fundamental Algorithms

Chapter 1

Section 1

Algorithm 1.1E - Euclid's Algorithm

#python #algorithm #implementation #taocp

"""Module to compute GCD(Greatest Common Divisor) of two numbers"""
def gcd(m, n):
"""Function to compute GCD of two numbers"""
if n > m:
m, n = n, m
r = m % n
if r == 0:
return n
return gcd(n, r)
from Algorithm_1_1_1E import gcd
assert gcd(51, 17) == 17
assert gcd(119, 544) == 17
assert gcd(12, 8) == 4
assert gcd(8, 8) == 8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment