Project Euler #1 Multiples of 3 or 5
fn main() {
let mut sum = 0;
for i in 1..1000 {
if i % 3 == 0 || i % 5 == 0 {
sum += i;
}
}
println!("The sum of all the multiples of 3 or 5 below 1000 is: {}", sum);
}
This Rust code calculates the sum of all the multiples of 3 or 5 below 1000. It uses a for
loop to iterate over the numbers from 1 to 999 (exclusive). The if
statement checks if the current number is divisible by 3 or 5 using the modulus operator %
. If it is, the number is added to the sum
variable. Finally, the result is printed using the println!
macro.