Skip to main content
Back to problems
#2652
Easy Algorithms

Sum multiples

Math
85.8% acceptance
Feb 25, 2026
590
44
Given a positive integer n, find the sum of all integers in the range [1, n] inclusive that are divisible by 3, 5, or 7. Return an integer denoting the sum of all numbers in the given range satisfying the constraint.

Solution

Rust
Time O(1)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn sum_of_multiples(n: i32) -> i32 {
    (1..=n).filter(|&i| i % 3 == 0 || i % 5 == 0 || i % 7 == 0).sum()
  }
}