Challenge - 5 Problems
Euclidean Algorithm Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of GCD calculation using Euclidean Algorithm
What is the output of the following JavaScript code that calculates the GCD of 48 and 18 using the Euclidean Algorithm?
DSA Javascript
function gcd(a, b) {
while (b !== 0) {
let temp = b;
b = a % b;
a = temp;
}
return a;
}
console.log(gcd(48, 18));Attempts:
2 left
💡 Hint
Remember the Euclidean Algorithm repeatedly replaces the larger number by the remainder of the division until the remainder is zero.
✗ Incorrect
The GCD of 48 and 18 is 6 because 6 is the largest number that divides both without remainder. The Euclidean Algorithm finds this by repeated modulo operations.
❓ Predict Output
intermediateOutput of LCM calculation using GCD
What is the output of this JavaScript code that calculates the LCM of 15 and 20 using their GCD?
DSA Javascript
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(15, 20));Attempts:
2 left
💡 Hint
LCM can be found by multiplying the two numbers and dividing by their GCD.
✗ Incorrect
The GCD of 15 and 20 is 5. The LCM is (15 * 20) / 5 = 60.
🧠 Conceptual
advancedUnderstanding the Euclidean Algorithm steps
Given two numbers 56 and 98, how many iterations will the Euclidean Algorithm take to find their GCD using the remainder method?
Attempts:
2 left
💡 Hint
Count each time the remainder operation is performed until remainder is zero.
✗ Incorrect
The steps are: 98 % 56 = 42 (1), 56 % 42 = 14 (2), 42 % 14 = 0 (3). So 3 iterations.
❓ Predict Output
advancedOutput of recursive GCD function
What is the output of this recursive JavaScript function call gcd(270, 192)?
DSA Javascript
function gcd(a, b) {
if (b === 0) return a;
return gcd(b, a % b);
}
console.log(gcd(270, 192));Attempts:
2 left
💡 Hint
Follow the recursive calls until b becomes zero.
✗ Incorrect
The recursive calls are gcd(270,192) -> gcd(192,78) -> gcd(78,36) -> gcd(36,6) -> gcd(6,0). The GCD is 6.
❓ Predict Output
expertOutput of LCM for large numbers using GCD
What is the output of this JavaScript code that calculates the LCM of 123456 and 789012?
DSA Javascript
function gcd(a, b) {
while (b !== 0) {
let temp = b;
b = a % b;
a = temp;
}
return a;
}
function lcm(a, b) {
return (a / gcd(a, b)) * b;
}
console.log(lcm(123456, 789012));Attempts:
2 left
💡 Hint
Calculate GCD first, then use formula LCM = (a / GCD) * b to avoid overflow.
✗ Incorrect
GCD(123456,789012) is 12. LCM = (123456 / 12) * 789012 = 10288 * 789012 = 8117355456.
