Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,855 questions

51,776 answers

573 users

How to find the sum of all the multiples of 3 or 5 below 1000 in Rust

2 Answers

0 votes
fn main() {
    let mut sum = 0;

    // Iterate through numbers from 0 to 999
    for x in 0..1000 {
        if x % 3 == 0 || x % 5 == 0 {
            sum += x;
        }
    }

    println!("{}", sum);
}




/*
run:

233168

*/

 



answered Apr 15, 2025 by avibootz
0 votes
fn main() {
    // Define the limit
    let limit: i32 = 999;

    // Calculate the upper bounds
    let upper_for_three: i32 = limit / 3;
    let upper_for_five: i32 = limit / 5;
    let upper_for_fifteen: i32 = limit / 15;

    // Calculate the sums using the arithmetic series formula
    let sum_three: i32 = 3 * upper_for_three * (1 + upper_for_three) / 2;
    let sum_five: i32 = 5 * upper_for_five * (1 + upper_for_five) / 2;
    let sum_fifteen: i32 = 15 * upper_for_fifteen * (1 + upper_for_fifteen) / 2;

    // Calculate the total sum
    let total_sum: i32 = sum_three + sum_five - sum_fifteen;

    println!("{}", total_sum);
}



/*
run:

233168

*/

 



answered Apr 15, 2025 by avibootz
...