Yordan Gergov’s Post

Euclidean Algorithm: 2300 years old, still the most elegant solution I've seen. This morning's Codewars problem: Find the Greatest Common Divisor of two numbers. My first instinct? Write a loop. Seven lines of code. It worked, but felt clunky. Then I thought: "This feels recursive." Spent 10 minutes staring at the screen. "What's the base case?" "How do I reduce the problem?" Then it clicked. The insight: GCD(a, b) is just GCD(b, remainder). Keep calling until the remainder is zero. That's your answer. Four lines instead of seven. Elegant instead of clunky. Recursive instead of iterative. What makes it beautiful: → Each step is simpler than the last → The solution emerges naturally → Base case is obvious (when b = 0) Traced through an example: GCD(48, 18) becomes GCD(18, 12) which becomes GCD(12, 6) which becomes GCD(6, 0) Answer: 6 Euclid figured this out in 300 BC. I figured it out this morning. Better late than never. This is why I solve algorithms daily. Not for interviews. For the mindset. Learning to see problems recursively changes how you code. #Algorithms #Recursion #Codewars #JavaScript #LearningInPublic

To view or add a comment, sign in

Explore content categories