3 Greatest Common Divisor GCD
Problem Statement
Write recursive code to calculate the Greatest Common Divisor (GCD) of Two Positive Numbers.
The greatest common divisor (GCD) of two positive integers A and B is the largest positive integer that divides both A and B without leaving a remainder.
Let's see some example inputs/outputs for this example:
| Input(s) | Output(s) | Explanation |
|---|---|---|
| A = 12, B = 18 | GCD = 6 | The factors of 12 are [1, 2, 3, 4, 6, 12], and the factors of 18 are [1, 2, 3, 6, 9, 18]. The common factors between 12 and 18 are [1, 2, 3, 6], and the largest common factor is 6. |
| A = 25, B = 15 | GCD = 5 | The factors of 25 are [1, 5, 25], and the factors of 15 are [1, 3, 5, 15]. The common factors between 25 and 15 are [1, 5], and the largest common factor is 5. |
| A = 40, B = 60 | GCD = 20 | The factors of 40 are [1, 2, 4, 5, 8, 10, 20, 40], and the factors of 60 are [1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30, 60]. The common factors between 40 and 60 are [1, 2, 4, 5, 10, 20], and the largest common factor is 20. |
Try it yourself
Try solving this question here:
Code
Here is the code for this algorithm:
🎯 STRICT STANDOUT — Problem cue: GCD (Euclid)
1. Why
Euclid gcd(a,b)=gcd(b,a%b), base b=0→a. Logarithmic depth — remainders shrink fast.
2. Big-O (K11)
Worst Fibonacci pair: O(log_φ a) steps still O(log a); stack O(log a)
Subtraction Euclid can be Θ(a) — avoid
3. Pattern + when-NOT
EUCLIDEAN RECURSION. When-NOT: subtraction form; binary GCD for bit ops sometimes.
4. Edge
gcd(48,18)=6; gcd(7,0)=7; gcd(0,0) specify; gcd(1,1)=1
5. Panel
Q: Why not O(n)? A: Modulo shrinks exponentially, not by 1.
🤖 Don't fully get this? Learn it with Claude
Stuck on 3 Greatest Common Divisor GCD? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.
Build the mental picture, not memorization.
I just read a lesson on **3 Greatest Common Divisor GCD** (DSA) and want to truly understand it. Explain 3 Greatest Common Divisor GCD from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Socratic — adapts to where you're stuck.
Teach me **3 Greatest Common Divisor GCD** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Active recall exposes what you missed.
Quiz me on **3 Greatest Common Divisor GCD** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Intuition + hook + flashcards for long-term memory.
Help me remember **3 Greatest Common Divisor GCD** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.