bool IsLucasProbablePrime(const Integer &n) { if (n <= 1) return false; if (n.IsEven()) return n==2; assert(n>2); Integer b=3; unsigned int i=0; int j; while ((j=Jacobi(b.Squared()-4, n)) == 1) { if (++i==64 && n.IsSquare()) // avoid infinite loop if n is a square return false; ++b; ++b; } if (j==0) return false; else return Lucas(n+1, b, n)==2; }
bool IsStrongLucasProbablePrime(const Integer &n) { if (n <= 1) return false; if (n.IsEven()) return n==2; assert(n>2); Integer b=3; unsigned int i=0; int j; while ((j=Jacobi(b.Squared()-4, n)) == 1) { if (++i==64 && n.IsSquare()) // avoid infinite loop if n is a square return false; ++b; ++b; } if (j==0) return false; Integer n1 = n+1; unsigned int a; // calculate a = largest power of 2 that divides n1 for (a=0; ; a++) if (n1.GetBit(a)) break; Integer m = n1>>a; Integer z = Lucas(m, b, n); if (z==2 || z==n-2) return true; for (i=1; i<a; i++) { z = (z.Squared()-2)%n; if (z==n-2) return true; if (z==2) return false; } return false; }