[Math] GCD & LCM

GCD(Greatest Common Divisor) & LCM(Least Common Multiple)



1. Find GCD of two number

Method 1: use basic Euclidean algorithm.
GCD(a, b) = GCD(b%a, a)
Method 2: use extended Euclidean algorithm.
GCD(a, b) = ax + by
Let values of x and y calculated by the recursive call be x1 and y1
x = y1 - ⌊b/a⌋ * x1
y = x1


2. Find LCM of two number

LCM(a, b) * GCD(a, b) = a * b

3. Reference

  • http://www.geeksforgeeks.org/mathematical-algorithms/#gcd

댓글

이 블로그의 인기 게시물

Android essentials summary

Useful Linux Command Line Bash Shortcuts You Should Know