When you have a problem like that you should break up a complex calculation into separate pieces so you can print the intermediate values.
As you are using an integer array I suspect you are suffering from overflow. Try changing its datatype to long.
...R