Dynamic Programming21 sections · 916 units
Open in Course

GCD - Problem Statement

Greatest Common Divisor

Given two positive integers aa and bb, find their greatest common divisor (GCD), the largest number that divides both. For example, gcd(48,18)=6\gcd(48, 18) = 6 because 66 divides both 4848 and 1818, and no larger number does.

You could try every number from 11 to min(a,b)\min(a, b) and check which ones divide both. That works but is slow. There is a recursive solution that is clean and fast. Before reading on, think: what relationship might exist between gcd(a,b)\gcd(a, b) and smaller inputs?

unnamed (11) (1).jpg