qwen-max-2025-01-25· Alibaba (Qwen) LMArena Human Preference 140K
write fizz buzz in rust
- User
write fizz buzz in rust
- Assistant
Here's how you can implement the FizzBuzz problem in Rust. The program will print numbers from 1 to 100, replacing multiples of 3 with "Fizz", multiples of 5 with "Buzz", and multiples of both 3 and 5 with "FizzBuzz".
fn main() { for i in 1..=100 { if i % 15 == 0 { println!("FizzBuzz"); } else if i % 3 == 0 { println!("Fizz"); } else if i % 5 == 0 { println!("Buzz"); } else { println!("{}", i); } } }Explanation:
- Loop through numbers:
for i in 1..=100iterates over numbers from 1 to 100 (inclusive). The..=range syntax includes the upper bound. - Check divisibility:
i % 15 == 0: Checks if the number is divisible by both 3 and 5 (since 15 is the least common multiple of 3 and 5).i % 3 == 0: Checks if the number is divisible by 3.i % 5 == 0: Checks if the number is divisible by 5.
- Print output:
- If divisible by both 3 and 5, print
"FizzBuzz". - If divisible by 3 only, print
"Fizz". - If divisible by 5 only, print
"Buzz". - Otherwise, print the number itself.
- If divisible by both 3 and 5, print
Output Example:
For the first 15 numbers, the output would look like this:
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzzThis implementation is concise, efficient, and adheres to idiomatic Rust practices.
- Loop through numbers:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.