Modular sum of square numbers in C++
To use the modulo in equations, You need to maintain the equivalence of the Equation. The Equation you wrote in comment is not according to equivalence. See Division Under Equivalences

So, going by the equivalence you have to calculate it like this :


  [ ( (n) %x ) * ( (n+1) %x ) * ( ( 2*n + 1 ) %x ) ) * ( (1/6)%x ) ] %x 


Have a look at this.

Note that this will not work If given n is any multiple of x. So, n &amp; x must be Co-prime. In your case, x = 10234573 is already a prime number as pointed by Toby's Comment