You are initialising (in the worst case) the variable i
to be a large number close to 200,000 (specifically a large prime number?). That means j
, which is initialised as i * i
, will be in excess of 40,000,000,000, which is significantly over the max value of int
(approx. 2,147,000,000) That means they will overflow over to negative values, which will definitely be out of range.
To solve the problem in this case, declare your variables to be of type long
which is 64-bit and can hold much larger values.