Find the largest prime p such that 1991p + 1 is a perfect square.

Did you write your question correctly? There seems to be no p that gives you a "perfect square". I tried all p's up 10,000,000 and couldn't find anything !!.

Yes, there is a solution! You just missed it.

a=1; b=2#(1991*a + 1);if(ceil(b)==floor(b) and isprime(a), goto3, goto4);printc, a; a++;if(a<1000000, goto1, 0)

OUTPUT =p =1993. [1991 * 1993 + 1]^1/2 =1992