If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6, and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below N.
Example:
Input: 10
Output: 23
Approach
C++
#include <bits/stdc++.h>using namespace std;long long starter(long long n){long long cnt = 0;n = n - 1;long long sum3 = (n / 3) * (2 * 3 + (n / 3 - 1) * 3) / 2;long long sum5 = (n / 5) * (2 * 5 + (n / 5 - 1) * 5) / 2;long long sum15 = (n / 15) * (2 * 15 + (n / 15 - 1) * 15) / 2;cnt = sum3 + sum5 - sum15;return cnt;}int main(){long long n = 10;cout << starter(n) << "\n";return 0;}
No comments:
Post a Comment