nextprime

버전 1.3.0.0 (3.31 KB) 작성자: John D'Errico
For any given number (also vpi numbers), find the next prime number in the sequence of primes.
다운로드 수: 2.7K
업데이트 날짜: 2009/5/14

라이선스 보기

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.

tic,nextprime(1000000000),toc
ans =
1000000007
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.

nextprime(1000:100:2000)
ans =
1009 1103 1201 1301 1409 1511 1601 1709 1801 1901 2003

You can search in either direction, above or below the starting point too.

nextprime(43420000,'above')
ans =
43420007

nextprime(43420000,'below')
ans =
43419977

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.

>> nextprime(2^47)
??? 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.

>> nextprime(vpi(2).^[12;47;53;86])
ans =
4099
140737488355333
9007199254740997
77371252455336267181195291

인용 양식

John D'Errico (2024). nextprime (https://www.mathworks.com/matlabcentral/fileexchange/23846-nextprime), MATLAB Central File Exchange. 검색 날짜: .

MATLAB 릴리스 호환 정보
개발 환경: R2007b
모든 릴리스와 호환
플랫폼 호환성
Windows macOS Linux
카테고리
Help CenterMATLAB Answers에서 Number Theory에 대해 자세히 알아보기
태그 태그 추가
도움

도움 받은 파일: Variable Precision Integer Arithmetic

도움 준 파일: nthprime

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
버전 게시됨 릴리스 정보
1.3.0.0

Modification to allow inputs up to 2^46 as numeric. Also did some minor cleanup, better error messages.