- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem001.cpp
More file actions
Latest commit
17 lines (15 loc) · 481 Bytes
/
Copy pathproblem001.cpp
File metadata and controls
17 lines (15 loc) · 481 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<iostream>
intsumOfMultiples(int n, int upper)
{
// n + 2n + 3n + ... + p = n * (1 + 2 + 3 + ... + (p / n))
// = n * ((p / n) * ((p / n) + 1) / 2)
int k = upper / n;
return n * (k * (k + 1) / 2);
}
intmain()
{
// Multiples of 15 are redundant, being multiples of both 3 and 5.
int ans = sumOfMultiples(3, 999) + sumOfMultiples(5, 999) - sumOfMultiples(15, 999);
std::cout << "Answer: " << ans << '\n';
return0;
}