4 ms·
https://godbolt.org/z/61xGW7 https://godbolt.org/z/61xGW7 Basically, the compiler isn't smart enough to realize that the loop in the factorial function couldn'
by nwallin 6y ago
https://godbolt.org/z/61xGW7 https://godbolt.org/z/61xGW7
Basically, the compiler isn't smart enough to realize that the loop in the factorial function couldn't possibly execute more than a few iterations before returning. Basically, it tries to do this:
int a = 1;
int b = 2;
int c = 3;
int d = 4;
for (int i = 5; i < x; i += 4) {
a *= i;
b *= i + 1;
c *= i + 2;
d *= i + 3;
}
// handwave a bunch of crap about edge cases
return a * b * c * d;