9 ms·
First it normalizes to if (num <= 0) { return 0; } int a = 0; int loop_n = (unsigned)(num - 1) / 5; for (int x = 0; x <= loop_n; x+
by Veedrac 10y ago
First it normalizes to
if (num <= 0) {
return 0;
}
int a = 0;
int loop_n = (unsigned)(num - 1) / 5;
for (int x = 0; x <= loop_n; x++) {
a += x * 5 - 1;
}
return a;
Division by 5 is done by multiplying by the modular inverse, 3435973837.
From there you just have a summation, which has two components.
((loop_n + 1) * loop_n / 2) * 5 // 5x
- loop_n - 1 // -1
The assembly between these the manual and automatic methods is slightly different, but the difference is fairly trivial and opaque.