📖 Scenario: Imagine you are helping a friend who loves baking. They want to find the greatest common divisor (GCD) and least common multiple (LCM) of two numbers to decide how to evenly divide ingredients or plan baking batches.
🎯 Goal: You will build a small Rust program that calculates the GCD and LCM of two given numbers using the Euclidean algorithm.
📋 What You'll Learn
Create two variables a and b with exact integer values
Create a variable original_a to store the initial value of a
Implement the Euclidean algorithm using a while loop to find the GCD
Calculate the LCM using the formula (original_a * b) / a
Print the GCD and LCM values exactly as specified
💡 Why This Matters
🌍 Real World
Finding GCD and LCM helps in tasks like dividing resources evenly or scheduling repeated events without overlap.
💼 Career
Understanding Euclidean algorithm and basic Rust programming is useful for software developers working on algorithms, embedded systems, or performance-critical applications.
Progress0 / 4 steps
1
Create the initial numbers
Create two variables called a and b with the exact values 48 and 18 respectively.
DSA Rust
Hint
Use let mut to create variables a and b with values 48 and 18.
2
Store the original value of a
Add a variable called original_a that stores the initial value of a.
DSA Rust
Hint
Use let original_a = a; to save the starting value of a.
3
Calculate the GCD using Euclidean algorithm
Use a while loop with the condition b != 0 to repeatedly update a and b using the Euclidean algorithm. Inside the loop, create a temporary variable temp to store b, then set b to a % b, and finally set a to temp.
DSA Rust
Hint
Use the Euclidean algorithm steps inside a while loop to find the GCD.
4
Calculate and print the GCD and LCM
Calculate the LCM using the formula (original_a * b) / a and store it in a variable called lcm. Then print the GCD and LCM using println! exactly as: "GCD: {} and LCM: {} with the values of a and lcm.
DSA Rust
Hint
Calculate LCM with (original_a * 18) / a and print both values with println!.
Practice
(1/5)
1. What does the Euclidean algorithm primarily calculate when given two positive integers a and b?
easy
A. The greatest common divisor (GCD) of a and b
B. The least common multiple (LCM) of a and b
C. The sum of a and b
D. The difference between a and b
Solution
Step 1: Understand Euclidean algorithm purpose
The Euclidean algorithm is designed to find the greatest common divisor (GCD) of two integers by repeatedly applying the remainder operation.
Step 2: Differentiate GCD from LCM
While GCD is the largest number dividing both without remainder, LCM is the smallest number divisible by both. Euclidean algorithm directly finds GCD.
Final Answer:
The greatest common divisor (GCD) of a and b -> Option A
Quick Check:
Euclidean algorithm = GCD [OK]
Hint: Euclidean algorithm finds GCD, not LCM or sums [OK]
Common Mistakes:
Confusing GCD with LCM
Thinking it calculates sum or difference
Assuming it returns both GCD and LCM
2. Which of the following Rust function signatures correctly represents a function to compute the GCD of two unsigned integers using the Euclidean algorithm?
easy
A. fn gcd(a: u32, b: u32) -> u32
B. fn gcd(a: i32, b: i32) -> i32
C. fn gcd(a: u32, b: u32) -> bool
D. fn gcd(a: u32, b: u32)
Solution
Step 1: Identify correct parameter and return types
GCD function takes two unsigned integers (u32) and returns an unsigned integer (u32) representing the GCD.
Step 2: Check function signature correctness
fn gcd(a: u32, b: u32) -> u32 matches this: inputs are u32, output is u32. fn gcd(a: i32, b: i32) -> i32 uses signed integers, which is less common for GCD. fn gcd(a: u32, b: u32) -> bool returns bool, incorrect. fn gcd(a: u32, b: u32) lacks return type.
Final Answer:
fn gcd(a: u32, b: u32) -> u32 -> Option A
Quick Check:
Correct signature returns u32 [OK]
Hint: GCD inputs and output are unsigned integers [OK]
Common Mistakes:
Using signed integers instead of unsigned
Missing return type in function signature
Returning wrong type like bool
3. What is the output of the following Rust code snippet?
fn gcd(mut a: u32, mut b: u32) -> u32 {
while b != 0 {
let temp = b;
b = a % b;
a = temp;
}
a
}
fn main() {
let result = gcd(48, 18);
println!("{}", result);
}
medium
A. 12
B. 6
C. 18
D. 3
Solution
Step 1: Trace Euclidean algorithm steps for gcd(48, 18)