Skip to content

Instantly share code, notes, and snippets.

@patrickaldis
Last active August 22, 2022 09:56
Show Gist options
  • Save patrickaldis/ef35d5069a0de7402b414e7bf41ebc40 to your computer and use it in GitHub Desktop.
Save patrickaldis/ef35d5069a0de7402b414e7bf41ebc40 to your computer and use it in GitHub Desktop.
`gcd` recursion

gcd Recursion

While testing gcd function I ended up needing to know a termination bound for gcd m n, so had to investigate how many steps the recursion took. The case I tested was for gcd 7 5 - it had enough steps. The defintion for gcd follows the euclidean algorithm, which reduces gcd a b to gcd b c
for a smaller c. At each stage, this reduction is simply c = a rem b

gcd 7 5

Algorithm terminates in 4 steps:

(7, 5) -> (5, 2) -> (2, 1) -> (1, 0). As hcf a 0 = a , hcf 7 5 = 1

Taking the ID from the metadata after evaluating:

gcd a b - metadata ID

gcd 7 5 - 2078

gcd 5 2 - 1189

gcd 2 1 - 564

gcd 1 0 - 276

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment