An adaptation of SQUFOF taken from Pari/GP.