This is actually a tool in my vpi toolbox, but it works quite nicely on any number. As such, I decided it would make sense to submit the function also separately.
Elapsed time is 0.006276 seconds.
A partial sieve scheme is used to avoid testing the primality of too many numbers. This makes it more efficient.
You can use it on lists of numbers too.
1009 1103 1201 1301 1409 1511 1601 1709 1801 1901 2003
You can search in either direction, above or below the starting point too.
The limit for nextprime when applied to double precision numbers is now 2^46. Thus you cannot find the next prime above 2^46, unless you are working with vpi numbers.
??? Error using ==> nextprime at 89
The maximum value of N (for numeric input) allowed is 2^46.
Instead, apply nextprime to any integer or set thereof as vpi numbers. This works perfectly, but you will need to install my vpi toolbox.
John D'Errico (2022). nextprime (https://www.mathworks.com/matlabcentral/fileexchange/23846-nextprime), MATLAB Central File Exchange. Retrieved .
MATLAB Release Compatibility
Platform CompatibilityWindows macOS Linux
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!