Challenge - 5 Problems
Euclidean Algorithm Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this Rust code for GCD calculation?
Consider the following Rust function that calculates the Greatest Common Divisor (GCD) using the Euclidean algorithm. What will be printed when calling gcd(48, 18)?
DSA Rust
fn gcd(mut a: u32, mut b: u32) -> u32 {
while b != 0 {
let temp = b;
b = a % b;
a = temp;
}
a
}
fn main() {
println!("{}", gcd(48, 18));
}Attempts:
2 left
💡 Hint
Remember the Euclidean algorithm repeatedly replaces the larger number by the remainder until the remainder is zero.
✗ Incorrect
The Euclidean algorithm finds the GCD by repeatedly taking remainders. gcd(48,18) reduces as 48%18=12, then gcd(18,12), 18%12=6, then gcd(12,6), 12%6=0, so GCD is 6.
❓ Predict Output
intermediateWhat is the output of this Rust code for LCM calculation?
Given the following Rust code that calculates the Least Common Multiple (LCM) using the GCD function, what will be printed when calling lcm(21, 6)?
DSA Rust
fn gcd(mut a: u32, mut b: u32) -> u32 {
while b != 0 {
let temp = b;
b = a % b;
a = temp;
}
a
}
fn lcm(a: u32, b: u32) -> u32 {
(a * b) / gcd(a, b)
}
fn main() {
println!("{}", lcm(21, 6));
}Attempts:
2 left
💡 Hint
LCM can be found by multiplying the numbers and dividing by their GCD.
✗ Incorrect
GCD of 21 and 6 is 3. LCM = (21*6)/3 = 126/3 = 42.
🧠 Conceptual
advancedWhy does the Euclidean algorithm for GCD always terminate?
The Euclidean algorithm repeatedly replaces the pair (a, b) with (b, a % b) until b becomes zero. Why does this process always end?
Attempts:
2 left
💡 Hint
Think about how the remainder changes each step.
✗ Incorrect
At each step, the remainder is strictly less than the previous divisor, so the numbers decrease and eventually reach zero, ending the loop.
🔧 Debug
advancedWhat error does this Rust code produce when calculating GCD?
Consider this Rust code snippet intended to compute the GCD. What error will it produce when compiled?
DSA Rust
fn gcd(mut a: u32, mut b: u32) -> u32 {
while b != 0 {
let temp = a % b;
b = temp;
a = b;
}
a
}
fn main() {
println!("{}", gcd(10, 5));
}Attempts:
2 left
💡 Hint
Check punctuation carefully in Rust code.
✗ Incorrect
The line 'let temp = a % b' is missing a semicolon, causing a syntax error.
🚀 Application
expertWhat is the value of variable 'result' after this Rust code runs?
This Rust code calculates the LCM of three numbers using the GCD function. What is the final value of 'result'?
DSA Rust
fn gcd(mut a: u32, mut b: u32) -> u32 {
while b != 0 {
let temp = b;
b = a % b;
a = temp;
}
a
}
fn lcm(a: u32, b: u32) -> u32 {
(a * b) / gcd(a, b)
}
fn main() {
let a = 4;
let b = 6;
let c = 8;
let result = lcm(lcm(a, b), c);
println!("{}", result);
}Attempts:
2 left
💡 Hint
Calculate LCM step by step: first lcm(a,b), then lcm of that with c.
✗ Incorrect
LCM(4,6) = 12. Then LCM(12,8) = (12*8)/GCD(12,8) = 96/4 = 24. So final result is 24.
