GCD and LCM#
The Greatest Common Divisor (GCD) of two numbers is the largest positive integer that divides both numbers without leaving a remainder. The Least Common Multiple (LCM) of two numbers is the smallest positive integer that is divisible by both numbers.
Finding the GCD#
To find the GCD of two numbers \(a\) and \(b\), we can use the Euclidean Algorithm:
Divide \(a\) by \(b\): Compute the quotient and remainder.
Replace \(a\) with \(b\) and \(b\) with the remainder: Repeat the process until the remainder is 0.
GCD is the last non-zero remainder.
Finding the LCM#
To find the LCM of two numbers \(a\) and \(b\), we can use the relationship between GCD and LCM:
### Functions:
Show code cell source
import sympy as sp
from sympy import *
from math import gcd
import random
def find_gcd_lcm(a, b):
gcd = sp.gcd(a, b)
lcm = a * b // gcd
print(f"GCD of {a} and {b} is {gcd}")
print(f"LCM of {a} and {b} is {lcm}")
Examples:#
Finding the GCD and LCM of \(44\) and \(12\)#
a = 44
b = 12
find_gcd_lcm(a, b)
GCD of 44 and 12 is 4
LCM of 44 and 12 is 132
Explanation
To find the GCD of \(44\) and \(12\), we can use the Euclidean Algorithm:
Divide \(44\) by \(12\):
Replace \(44\) with \(12\) and \(12\) with \(8\):
Replace \(12\) with \(8\) and \(8\) with \(4\):
The GCD is the last non-zero remainder:
Finding the LCM
To find the LCM of \(44\) and \(12\), use the relationship between GCD and LCM:
Finding the GCD and LCM of \(15\) and \(25\)#
a = 15
b = 25
find_gcd_lcm(a, b)
GCD of 15 and 25 is 5
LCM of 15 and 25 is 75
Explanation
To find the GCD of \(15\) and \(25\), we can use the Euclidean Algorithm:
Divide \(25\) by \(15\):
Replace \(25\) with \(15\) and \(15\) with \(10\):
Replace \(15\) with \(10\) and \(10\) with \(5\):
The GCD is the last non-zero remainder:
Finding the LCM
To find the LCM of \(15\) and \(25\), use the relationship between GCD and LCM:
Finding the GCD and LCM of \(30\) and \(45\)#
a = 30
b = 45
find_gcd_lcm(a, b)
Explanation
To find the GCD of \(30\) and \(45\), we can use the Euclidean Algorithm:
Divide \(45\) by \(30\):
Replace \(45\) with \(30\) and \(30\) with \(15\):
The GCD is the last non-zero remainder:
Finding the LCM
To find the LCM of \(30\) and \(45\), use the relationship between GCD and LCM:
Finding the GCD and LCM of \(21\) and \(14\)#
a = 21
b = 14
find_gcd_lcm(a, b)
Explanation
To find the GCD of \(21\) and \(14\), we can use the Euclidean Algorithm:
Divide \(21\) by \(14\):
Replace \(21\) with \(14\) and \(14\) with \(7\):
The GCD is the last non-zero remainder:
Finding the LCM
To find the LCM of \(21\) and \(14\), use the relationship between GCD and LCM: