[LeetCode] Sum Multiples

2652. Sum Multiples

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.

1
2
3
4
5
6
7
8
9
10
class Solution {
public:
int sumOfMultiples(int n) {
int res = 0;
for(int i = 1; i <= n; i++) {
if(i % 3 == 0 or i % 5 == 0 or i % 7 == 0) res += i;
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/04/23/PS/LeetCode/sum-multiples/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.