Greatest Common Divisor or Highest Common Factor
Math
Write an algorithm to determine the GCD of N positive numbers.
Analysis & Solution
欧几里得算法求最大公约数
关于GCD算法,自然是有欧几里得算法Euclid Algorithm。不过实现上,可以是迭代,或者递归。
Pseudo Code:
Iterative
Recursive:
Iterative (Java)
Recursive (Java)
Euclid Algorithm Time Complexity
这是一个挺复杂的数学问题,作为估计(也是实际上的),可以认为迭代次数O(logN)
如何从两个数的GCD拓展到N个数呢?
You could use this common property of a GCD:
GeeksforGeeks:
For an array of elements:
Implementation:
Reference
Last updated
Was this helpful?