Euclidean Algorithm in Number Theory
Euclidean Algorithm
The Euclidean Algorithm is an efficient method for finding the Greatest Common Divisor (GCD) of two integers. It uses the principle that the GCD of two numbers does not change if the larger number is replaced by its remainder when divided by the smaller number.
Video Explanationโ

Steps to Implementโ
- Divide the larger number by the smaller number and find the remainder.
- Replace the larger number with the smaller number and the smaller number with the remainder.
- Repeat until the remainder is 0. The non-zero remainder is the GCD.
Code Examplesโ
C++ Implementationโ
#include <iostream>
using namespace std;
int gcd(int a, int b) {
while (b != 0) {
int remainder = a % b;
a = b;
b = remainder;
}
return a;
}
int main() {
int a, b;
cout << "Enter two integers: ";
cin >> a >> b;
cout << "GCD of " << a << " and " << b << " is: " << gcd(a, b) << endl;
return 0;
}
Python Implementationโ
def gcd(a, b):
while b != 0:
a, b = b, a % b
return a
if __name__ == "__main__":
a = int(input("Enter the first integer: "))
b = int(input("Enter the second integer: "))
print(f"GCD of {a} and {b} is: {gcd(a, b)}")
Example Walkthroughโ
Example 1: GCD of 56 and 98โ
Example 2: GCD of 101 and 103โ