Problem 802
Sum of Multiples of 3 or 5
Easy · 10 pts · Math, Basics, Inclusion-Exclusion
Problem
Add up all the natural numbers below N (strictly less than N) that are multiples of 3 or 5. A number that is a multiple of both, like 15, is counted only once. A loop works for small N; for N up to 10^9 use the formula for the sum of multiples of k below N and inclusion-exclusion: sum(3) + sum(5) - sum(15). The numbers in this problem can be larger than a 32-bit int can hold (about 2.1 × 10^9), so use a 64-bit integer type: long long in C++, long in Java (Python needs nothing special).
Input
A single integer N.
Output
Print the sum of the multiples of 3 or 5 below N.
Constraints
1 <= N <= 10^9
Example input
10
Example output
23
Explanation
Below 10 the multiples of 3 or 5 are 3, 5, 6 and 9, and their sum is 23.