Instead of using an int try using a longto store your result.

This means changing the array p to type long and changing the variable newTerms to long