Answers for "Project Euler #1 Multiples of 3 or 5"

0

Project Euler #1 Multiples of 3 or 5

// Project Euler #1 Multiples of 3 or 5
// num is the maximum number e.g. 1000
// Iterative
fn solution(num: i32) -> i32 {
    let mut sum = 0;
    for i in 1 .. num {
        if i % 3 == 0 || i % 5 == 0 {
            sum += i;
        }
    }
    sum
}

// Using filter
fn solution(num: i32) -> i32 {
  (1..num).filter(|x| x % 3 == 0 || x % 5 == 0 ).sum()
}
Posted by: Guest on April-19-2022

Code answers related to "Project Euler #1 Multiples of 3 or 5"

Browse Popular Code Answers by Language