Replace "sum" with the correct operator "add"

Also fix array indexing error.  Upper limits of for loops were p when they
should have been p - 1, causing us to try to acces index p of an array of size p.