TheAlgorithms/C-Sharp · error · ArgumentException
should be more than 3
Error message
{nameof(n)} should be more than 3 What it means
The private Miller-Rabin helper IsProbablyPrimeNumber implements the randomized primality test, whose algorithm requires n > 3 (small values are handled by the public wrapper). It throws ArgumentException when n <= 3. This is an internal invariant; hitting it usually means the public entry point did not screen small inputs.
Solutions
- Handle n <= 3 at the call site: 2 and 3 are prime, 0, 1 and negatives are not; only pass n > 3.
- Ensure you call the public API method rather than the private helper.
- Pre-screen with a small table of primes/composites before the probabilistic test.
Example fix
// before
bool result = checker.IsProbablyPrimeNumber(candidate);
// after
bool result = candidate <= 3
? candidate is 2 or 3
: checker.IsProbablyPrimeNumber(candidate); Defensive patterns
Strategy: validation
Validate before calling
bool IsPrimeSmall(long n) => n switch { 2 or 3 => true, <= 1 => false, _ => Checker.IsProbablyPrimeNumber(n) }; Try / catch
try { return IsProbablyPrimeNumber(n, rounds, rand); }
catch (ArgumentException ex) when (ex.Message.Contains("more than 3")) { return n is 2 or 3; } Prevention
- Short-circuit n <= 3 before the probabilistic test: 2,3 prime; 0,1,negative composite-by-definition
- Always go through the public wrapper which handles small inputs
- Cover tiny inputs (0,1,2,3,4) in primality unit tests
When it happens
Trigger: The public primality-check path forwarding n <= 3 (e.g. 2, 3, 0, 1, or negatives) into IsProbablyPrimeNumber instead of short-circuiting them.
Common situations: Calling the checker with tiny numbers like 2 or 3 (which are prime by definition), 1, or negatives; or refactoring that bypasses the public wrapper's small-number handling.
Understand the failure class
Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.
Related errors
- Invalid parameter settings for Ascon Hash
- Cash flows list cannot be empty
- cannot be negative
- The lower bound must be less than or equal to the upper…
- An automorphic number must always be positive.
AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13).
Data as JSON: /api/errors/4bc1183f908338cb.
Report an issue: GitHub.
Appendix: source
Thrown at Algorithms/Numeric/MillerRabinPrimalityChecker.cs:33
/// </summary>
/// <param name="n">Number to check.</param>
/// <param name="rounds">Number of rounds, the parameter determines the accuracy of the test, recommended value is Log2(n).</param>
/// <param name="seed">Seed for random number generator.</param>
/// <returns>True if is a highly likely prime number; False otherwise.</returns>
/// <exception cref="ArgumentException">Error: number should be more than 3.</exception>
public static bool IsProbablyPrimeNumber(BigInteger n, BigInteger rounds, int? seed = null)
{
Random rand = seed is null
? new()
: new(seed.Value);
return IsProbablyPrimeNumber(n, rounds, rand);
}
private static bool IsProbablyPrimeNumber(BigInteger n, BigInteger rounds, Random rand)
{
if (n <= 3)
{
throw new ArgumentException($"{nameof(n)} should be more than 3");
}
// Input #1: n > 3, an odd integer to be tested for primality
// Input #2: k, the number of rounds of testing to perform, recommended k = Log2(n)
// Output: false = “composite”
// true = “probably prime”
// write n as 2r·d + 1 with d odd(by factoring out powers of 2 from n − 1)
BigInteger r = 0;
BigInteger d = n - 1;
while (d % 2 == 0)
{
r++;
d /= 2;
}
// as there is no native random function for BigInteger we suppose a random int number is sufficient
int nMaxValue = (n > int.MaxValue) ? int.MaxValue : (int)n;View on GitHub (pinned to 96e2905cab)