%mathpiper,def="NextPrime"

/// obtain the real next prime number -- use primality testing
1# NextPrime(_i) <--
[
	Until(IsPrime(i)) i := NextPseudoPrime(i);
	i;
];

%/mathpiper



%mathpiper_docs,name="NextPrime",categories="User Functions;Number Theory"
*CMD NextPrime --- generate a prime following a number
*STD
*CALL
	NextPrime(i)

*PARMS

{i} -- integer value

*DESC

The function finds the smallest prime number that is greater than the given
integer value.

The routine generates "candidate numbers" using the formula $n+2*Modulo(-n,3)$
where $n$ is an odd number (this generates the sequence 5, 7, 11, 13, 17,
19, ...) and {IsPrime()} to test whether the next candidate number is in
fact prime.

*E.G.
In> NextPrime(5)
Result: 7;

*SEE IsPrime
%/mathpiper_docs