Welcome to Day 57 of the #80DaysOfChallenges journey! This intermediate challenge brings you one of the oldest and most powerful algorithms in human history: Euclid’s algorithm for Greatest Common Divisor (GCD), written over 2300 years ago in ancient Greece, and still the fastest way to compute GCD and LCM in 2025. We implement it manually (no math.gcd allowed!) with the ultra-Pythonic tuple swap, then use it to calculate Least Common Multiple (LCM) via the relationship LCM(a,b) = |a×b| / GCD(a,b).
This is the exact method used in cryptography (RSA), computer graphics, fraction simplification, and every modern math library. It’s O(log min(a,b)) time, unbeatable efficiency.
If you’re into number theory, preparing for interviews, or just love algorithms th…
Welcome to Day 57 of the #80DaysOfChallenges journey! This intermediate challenge brings you one of the oldest and most powerful algorithms in human history: Euclid’s algorithm for Greatest Common Divisor (GCD), written over 2300 years ago in ancient Greece, and still the fastest way to compute GCD and LCM in 2025. We implement it manually (no math.gcd allowed!) with the ultra-Pythonic tuple swap, then use it to calculate Least Common Multiple (LCM) via the relationship LCM(a,b) = |a×b| / GCD(a,b).
This is the exact method used in cryptography (RSA), computer graphics, fraction simplification, and every modern math library. It’s O(log min(a,b)) time, unbeatable efficiency.
If you’re into number theory, preparing for interviews, or just love algorithms that stood the test of millennia, this "Python Euclidean GCD" is pure gold.
💡 Key Takeaways from Day 57: Euclidean GCD & LCM Functions
This challenge delivers two perfect functions: a mind-blowingly simple GCD using Euclid’s reduction, and LCM built on top. We’ll break it down: gcd with tuple swap magic, lcm with overflow-safe formula, and interactive main.
1. GCD Function: Euclid’s Genius in 4 Lines
def gcd(a: int, b: int) -> int:
"""Return the greatest common divisor using Euclid's algorithm."""
while b != 0:
a, b = b, a % b
return a
This is the single most beautiful line in all of mathematics translated to code:
a, b = b, a % b
The proof: GCD(a,b) = GCD(b, a mod b), and it terminates when b=0.
For 48 and 18: 48,18 → 18,12 → 12,6 → 6,0 → returns 6
Handles any order, negatives via abs if needed, but here assumes positive.
This runs in O(log min(a,b)) iterations, faster than anything else.
2. LCM Function: One-Line Power Using GCD
def lcm(a: int, b: int) -> int:
"""Return the least common multiple using the GCD result."""
return abs(a * b) // gcd(a, b)
The mathematical identity LCM(a,b) × GCD(a,b) = |a × b| lets us compute LCM instantly.
abs(a * b) prevents negative issues, // integer division for exact result.
For 12 and 18: GCD=6 → LCM = |216| // 6 = 36 ✓
Zero handling: if either is 0, LCM=0 (mathematically correct).
3. Main Interactive: Real Input Testing
raw_a = input("Enter first positive integer: ").strip()
raw_b = input("Enter second positive integer: ").strip()
if not raw_a or not raw_b:
print("Invalid input. Exiting.")
else:
a = int(raw_a)
b = int(raw_b)
result_gcd = gcd(a, b)
result_lcm = lcm(a, b)
print(f"GCD: {result_gcd}")
print(f"LCM: {result_lcm}")
Clean, safe, immediate results. Try 1071 and 462 → GCD=21, LCM=23562
🎯 Summary and Reflections
These GCD and LCM functions are perfect: mathematically proven, maximally efficient, and beautifully simple. It reinforced:
- Euclid’s algorithm is eternal: 2300+ years old and still the best
- Tuple swap is Python magic: a, b = b, a%b is unbeatably clean
- Math identities rule: LCM from GCD is pure genius
This code is used in real cryptography (key generation), games (timing), and everywhere fractions appear.
Advanced Alternatives:
- Recursive version: def gcd(a,b): return a if b==0 else gcd(b,a%b)
- Extended Euclidean for modular inverse
- Built-in math.gcd (Python 3.5+), but doing it manually teaches everything
But this iterative version wins for speed and clarity.
🚀 Next Steps and Resources
Day 57 brought ancient Greek math to modern Python. Try finding GCD of 2^100 and 3^100 if you dare!
- Source Code for Challenge #57: scripts/euclidean_gcd.py
- Main Repository: 80-days-of-challenges
- Daily Updates: Twitter/X (@Shahrouzlogs)
What’s the coolest thing you ever used GCD for? Tell me below! 🧮