Closed trizen closed 1 year ago
An optimization intended for detecting primes was hard-coding the answer for single digit n. Removed.
In cases where base >= n, we didn't check for base mod n == {0,1,n-1} the way the MPU code does. Will investigate.
The MPU code has a very early exit for n=2,3 which is not right for n=3 with bases >= 3. Will fix.
Making sure all three (XS, PP, GMP) match. Same for is_euler_pseudoprime.
@trizen I'm trying to do the same with is_strong_pseudoprime(n,base) and there's an interesting question here. The XS code implements the basic definition. But that means things like 367 -- a prime -- will return 0 when given a base = 0 mod 367, e.g. 1101. This seems really wrong as we don't want to return 0 for primes even with screwy bases.
The GMP code made a decision to change this slightly, and return 1 for base = 0 mod n. This means primes will always have a 1 returned regardless of the base. This is important to do for things like large deterministic base sets, though obviously code doing that operation can do the check itself. I like the idea of primes always returning 1, but that doesn't strictly follow the definition.
I think most sources skim over this by insisting the base be less than n. Or in Pari/GP's ispseudoprime it doesn't take a user input base. Wolfram/Mathworld writes:
The algorithm proceeds as follows. Given an odd integer n, let n=2^rs+1 with s odd. Then choose a random integer a with 1<=a<=n-1. If a^s=1 (mod n) or a^(2^js)=-1 (mod n) for some 0<=j<=r-1, then n passes the test. A prime will pass the test for all a.
The restrict the base, and note primes always pass. Which happens because they don't let a = 0 mod n by definition.
Changing either code will make a change to the behavior. Thoughts?
For some inputs,
is_pseudoprime(n, b)
seems to return0
, even ifn
is a Fermat pseudoprime to baseb
.Example (OEIS: A020138):
Notice that
Math::Prime::Util
works correctly for these inputs: