Problem 1: Multiples of 3 and 5
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 1000.
I first solved this problem September 1st, 2014 in my AP Computer Science class in highschool. I do not have the original code I wrote, but I have redone the solution and included it below.
This is a very simple and classic problem, commonly referred to as the "Fizz-Buzz test" used in job interviews in order to weed out applicants. More optimization could be done to make the program run faster, but I prefer this very clear solution.
#include <iostream>
int main() {
int sum = 0;
for (int i = 0; i < 1000; i++) {
if (i % 3 == 0 || i % 5 == 0) sum += i;
}
std::cout << sum << std::endl;
}
Answer: 233168