http://mathandmultimedia.com/2010/09/15/sum-first-n-positive...
magic constants galore... where does 3435973837 come from? ;)
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.[edit] and playing around with the increment of x it keeps throwing in some magical values which are a repeating pattern in hex, with the LSB off by one usually...
0.CCCCCCCC.... = 4/5 had anything to do with it.
Using hexadecimal we have
5 * 3 = 10 - 1, so
5 * C = 40 - 4 so
5 * (1 + C + C0 + C00 + C000...) = 5 - 4 + 40 - 40 + 400... where the last term is = 0 mod 2^32
-----
D
This relates to 0.CCCCCC.... = 4 / 5
Because in order for that to come out right, multiplying 0.CCCC... with 5 has to have all intermediate terms cancel, leaving only a 4.So why would it take its digit from 4 / 5 and not 1 / 5? Because it takes x=4 to make the 5-x subtraction in 5 - x + x0 - x0 + x00 to come out to 1.
So this gives us a general rule for inversion (for numbers less than 16). Take the hexadecimal expansion of (n-1) / n, truncate to get enough hex chars, add 1 to the least significant hex digit.
I.e. the inversion of 3 would be 0xAAAAAAAB