Skip to content

ext/gmp: Add gmp_prevprime()#22807

Open
LamentXU123 wants to merge 7 commits into
php:masterfrom
LamentXU123:gmp-prevprime
Open

ext/gmp: Add gmp_prevprime()#22807
LamentXU123 wants to merge 7 commits into
php:masterfrom
LamentXU123:gmp-prevprime

Conversation

@LamentXU123

Copy link
Copy Markdown
Member

Implement #13661

This is built on top of #16613

@ndossche

Copy link
Copy Markdown
Member

I think we should throw when the previous prime does not exist rather than return a GMP 0 number. 0 isn't a prime after all...

@TimWolla

Copy link
Copy Markdown
Member

Also according to GMP’s documentation (https://gmplib.org/manual/Number-Theoretic-Functions#index-mpz_005fprevprime):

Return 1 if rop is a probably prime, and 2 if rop is definitely prime.

These functions use a probabilistic algorithm to identify primes. For practical purposes it’s adequate, the chance of a composite passing will be extremely small.

@LamentXU123

Copy link
Copy Markdown
Member Author

mathematically it makes no sense to output a prime number less than 2. I'd suggest to directly throw ValueError in these cases in pure PHP spirits.

@LamentXU123
LamentXU123 requested a review from devnexen July 20, 2026 08:13
@LamentXU123
LamentXU123 marked this pull request as draft July 20, 2026 08:42
@LamentXU123
LamentXU123 marked this pull request as ready for review July 20, 2026 09:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants