Code Review
Despite its name, "stdafx.h"
isn't a standard header. It doesn't seem to be needed, as the code compiles and runs without it.
Your indentation is off - perhaps that's a result of how you inserted the code into the question?
The continue
is redundant - there's no more code within the loop after the if
/else
, so it can be omitted.
The code assumes that a result will be found before int
overflows. Remember that INT_MAX
may be as small as 32767; perhaps unsigned long
may be a better choice, or one of the fixed-width types specified in <cstdint>
, perhaps?
Your loop structure would be clearer if you separate out the test from incrementing smallest multiple. That could look something like this (still using int
as our type):
#include <iostream>
#include <climits>
static bool is_multiple_of_all(int n, int max)
{
for (int i = 10; i <= max; ++i) {
if (n % i != 0) {
return false;
}
}
return true;
}
int main()
{
const int target = 20;
for (int candidate = 2 * target;
candidate <= INT_MAX - target;
candidate += target)
{
if (is_multiple_of_all(candidate, target)) {
std::cout << candidate;
return 0;
}
}
// not found
return 1;
}
Algorithm
Project Euler problems tend to exercise your mathematical reasoning. You're expected to find better methods than brute force search to find the solution. (Hint: think about prime factors).