What does GCD stand for and what does it represent?
GCD stands for Greatest Common Divisor. It is the largest number that divides two or more numbers without leaving a remainder.
Click to reveal answer
beginner
What is the Euclidean Algorithm used for?
The Euclidean Algorithm is a method to find the GCD of two numbers by repeatedly subtracting the smaller number from the larger one or using the remainder operation until the remainder is zero.
Click to reveal answer
intermediate
How is LCM related to GCD?
LCM (Least Common Multiple) and GCD are related by the formula: LCM(a, b) = (a × b) / GCD(a, b). This means the product of two numbers equals the product of their GCD and LCM.
Click to reveal answer
intermediate
Explain the step-by-step process of the Euclidean Algorithm for GCD(48, 18).
1. Divide 48 by 18, remainder is 12 (48 = 18 × 2 + 12). 2. Divide 18 by 12, remainder is 6 (18 = 12 × 1 + 6). 3. Divide 12 by 6, remainder is 0 (12 = 6 × 2 + 0). When remainder is 0, the divisor (6) is the GCD.
Click to reveal answer
beginner
Write a simple JavaScript function to find GCD using the Euclidean Algorithm.
function gcd(a, b) {
while (b !== 0) {
const temp = b;
b = a % b;
a = temp;
}
return a;
}
Click to reveal answer
What is the GCD of 24 and 36?
A36
B6
C12
D24
✗ Incorrect
The largest number that divides both 24 and 36 without remainder is 12.
Which operation is repeatedly used in the Euclidean Algorithm?
AMultiplication
BSubtraction
CAddition
DModulo (remainder)
✗ Incorrect
The Euclidean Algorithm uses the modulo operation to find the remainder until it reaches zero.
If GCD(8, 12) = 4, what is LCM(8, 12)?
A48
B24
C16
D36
✗ Incorrect
LCM = (8 × 12) / 4 = 96 / 4 = 24.
What is the first step in the Euclidean Algorithm for GCD(56, 15)?
ADivide 56 by 15 and find remainder
BSubtract 15 from 56
CDivide 15 by 56
DAdd 56 and 15
✗ Incorrect
Start by dividing the larger number (56) by the smaller (15) and find the remainder.
Which of these is true about GCD and LCM?
ALCM is always a multiple of both numbers
BGCD is always greater than LCM
CGCD is always equal to LCM
DLCM is always smaller than both numbers
✗ Incorrect
LCM is the smallest number that both numbers divide into without remainder, so it is a multiple of both.
Describe how the Euclidean Algorithm finds the GCD of two numbers.
Think about dividing and using the remainder repeatedly.
You got /4 concepts.
Explain the relationship between GCD and LCM and how to calculate one if you know the other.
Remember the multiplication connection between GCD and LCM.
You got /3 concepts.
Practice
(1/5)
1. What does the Euclidean algorithm primarily calculate when given two positive integers a and b?
easy
A. The difference between a and b
B. The least common multiple (LCM) of a and b
C. The sum of a and b
D. The greatest common divisor (GCD) of a and b
Solution
Step 1: Understand the Euclidean algorithm purpose
The Euclidean algorithm is a method to find the greatest common divisor (GCD) of two numbers by repeatedly applying the remainder operation.
Step 2: Differentiate GCD from LCM
While GCD finds the largest number dividing both, LCM finds the smallest number divisible by both. Euclidean algorithm is for GCD.
Final Answer:
The greatest common divisor (GCD) of a and b -> Option D
Quick Check:
Euclidean algorithm = GCD [OK]
Hint: Euclidean algorithm always finds GCD, not LCM [OK]
Common Mistakes:
Confusing GCD with LCM
Thinking it calculates sum or difference
Assuming it finds prime factors directly
2. Which of the following JavaScript function definitions correctly implements the Euclidean algorithm to find the GCD of two numbers a and b?
easy
A. function gcd(a, b) { return a + b; }
B. function gcd(a, b) { while(b !== 0) { let temp = b; b = a % b; a = temp; } return a; }
C. function gcd(a, b) { return Math.min(a, b); }
D. function gcd(a, b) { while(a !== 0) { a = b % a; b = a; } return b; }
Solution
Step 1: Analyze the correct Euclidean algorithm loop
function gcd(a, b) { while(b !== 0) { let temp = b; b = a % b; a = temp; } return a; } uses a while loop that continues until b is zero, swapping values and using modulo to find remainder, which matches the Euclidean algorithm.
Step 2: Check other options for correctness
function gcd(a, b) { return a + b; } returns sum, not GCD. function gcd(a, b) { return Math.min(a, b); } returns minimum, not GCD. function gcd(a, b) { while(a !== 0) { a = b % a; b = a; } return b; } has incorrect variable updates causing infinite loop or wrong result.
Final Answer:
function gcd(a, b) { while(b !== 0) { let temp = b; b = a % b; a = temp; } return a; } -> Option B
Quick Check:
Correct Euclidean loop = function gcd(a, b) { while(b !== 0) { let temp = b; b = a % b; a = temp; } return a; } [OK]
Hint: Use while loop with modulo and swap variables [OK]
3. What is the output of the following JavaScript code?
function gcd(a, b) {
while (b !== 0) {
let temp = b;
b = a % b;
a = temp;
}
return a;
}
console.log(gcd(48, 18));
medium
A. 3
B. 12
C. 6
D. 18
Solution
Step 1: Trace the Euclidean algorithm steps for gcd(48, 18)
48 % 18 = 12, then gcd(18, 12); 18 % 12 = 6, then gcd(12, 6); 12 % 6 = 0, then gcd(6, 0) = 6.
Step 2: Return the last non-zero remainder
When second number becomes zero, the first number is the GCD, which is 6 here.
Final Answer:
6 -> Option C
Quick Check:
GCD(48,18) = 6 [OK]
Hint: Apply modulo repeatedly until remainder is zero [OK]
Common Mistakes:
Stopping too early and returning wrong remainder
Confusing modulo with division
Mixing up order of parameters
4. The following code attempts to compute the LCM of two numbers using their GCD. Identify the bug:
function gcd(a, b) {
while (b !== 0) {
let temp = b;
b = a % b;
a = temp;
}
return a;
}
function lcm(a, b) {
return (a * b) / gcd(a, b);
}
console.log(lcm(4, 6));
medium
A. The gcd function modifies a and b directly, affecting lcm calculation
B. The lcm function should add instead of multiply a and b
C. The gcd function should return b instead of a
D. There is no bug; the code correctly calculates LCM
Solution
Step 1: Understand variable mutation in gcd function
The gcd function changes a and b inside the loop, so after calling gcd, a and b in lcm are modified.
Step 2: Impact on lcm calculation
Since a and b are changed by gcd, the multiplication a * b in lcm uses modified values, causing incorrect LCM.
Final Answer:
The gcd function modifies a and b directly, affecting lcm calculation -> Option A
Quick Check:
Mutating inputs in gcd breaks lcm formula [OK]
Hint: Avoid changing input variables when computing GCD for LCM [OK]
Common Mistakes:
Assuming gcd doesn't change inputs
Using addition instead of multiplication in LCM
Returning wrong variable from gcd
5. Given an array of positive integers [12, 15, 20], which JavaScript function correctly computes the LCM of all elements using the Euclidean algorithm for GCD?
hard
A. function gcd(a, b) { while(b !== 0) { let t = b; b = a % b; a = t; } return a; } function lcm(a, b) { return (a * b) / gcd(a, b); } function lcmArray(arr) { return arr.reduce((acc, val) => lcm(acc, val)); } console.log(lcmArray([12, 15, 20]));
B. function gcd(a, b) { return a + b; } function lcm(a, b) { return a * b; } function lcmArray(arr) { return arr.reduce((acc, val) => lcm(acc, val)); } console.log(lcmArray([12, 15, 20]));
C. function gcd(a, b) { while(a !== 0) { a = b % a; b = a; } return b; } function lcm(a, b) { return (a + b) / gcd(a, b); } function lcmArray(arr) { return arr.reduce((acc, val) => lcm(acc, val)); } console.log(lcmArray([12, 15, 20]));
D. function gcd(a, b) { while(b !== 0) { let t = b; b = a % b; a = t; } return a; } function lcm(a, b) { return (a * b) * gcd(a, b); } function lcmArray(arr) { return arr.reduce((acc, val) => lcm(acc, val)); } console.log(lcmArray([12, 15, 20]));
Solution
Step 1: Verify gcd and lcm functions correctness
function gcd(a, b) { while(b !== 0) { let t = b; b = a % b; a = t; } return a; } function lcm(a, b) { return (a * b) / gcd(a, b); } function lcmArray(arr) { return arr.reduce((acc, val) => lcm(acc, val)); } console.log(lcmArray([12, 15, 20])); uses correct Euclidean gcd and calculates lcm as (a*b)/gcd(a,b), which is mathematically correct.
Step 2: Check lcmArray function logic
It uses reduce to apply lcm pairwise across the array, correctly computing LCM of all elements.
Step 3: Validate other options
function gcd(a, b) { return a + b; } function lcm(a, b) { return a * b; } function lcmArray(arr) { return arr.reduce((acc, val) => lcm(acc, val)); } console.log(lcmArray([12, 15, 20])); uses incorrect gcd and lcm formulas. function gcd(a, b) { while(a !== 0) { a = b % a; b = a; } return b; } function lcm(a, b) { return (a + b) / gcd(a, b); } function lcmArray(arr) { return arr.reduce((acc, val) => lcm(acc, val)); } console.log(lcmArray([12, 15, 20])); has wrong gcd and lcm formulas. function gcd(a, b) { while(b !== 0) { let t = b; b = a % b; a = t; } return a; } function lcm(a, b) { return (a * b) * gcd(a, b); } function lcmArray(arr) { return arr.reduce((acc, val) => lcm(acc, val)); } console.log(lcmArray([12, 15, 20])); incorrectly multiplies gcd in lcm formula.
Final Answer:
Option A code correctly computes LCM of array elements -> Option A
Quick Check:
Correct gcd, lcm, and reduce for array LCM [OK]
Hint: Use reduce with correct gcd and lcm formulas for arrays [OK]
Common Mistakes:
Incorrect gcd or lcm formulas
Not using reduce to combine array elements
Multiplying gcd instead of dividing in lcm
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.