hmm, i got similar (probably the same) mathematical closed form formula (see it in my first post, writen in Mathematica syntax) but i was not sure how to mathematically calculate the int overflow. I see you used mod 2^32, but that won't explain how you get negative answers, which happens with int ov...