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: n = 10
Output: 23
Approach
C++
#include <bits/stdc++.h>using namespace std;long long easyMultiple(long long n){n = n - 1;long long a = n / 3;long long b = n / 5;long long c = n / 15;long long ans = 0;ans += ((a) * (2 * 3 + (a - 1) * 3)) / 2;ans += (b * (2 * 5 + (b - 1) * 5)) / 2;ans -= (c * (2 * 15 + (c - 1) * 15)) / 2;return ans;}int main(){long long n = 10;cout << easyMultiple(n) << "\n";return 0;}
No comments:
Post a Comment