Function to find the next prime number...

조회 수: 26 (최근 30일)
Jyothi Alugolu
Jyothi Alugolu 2018년 2월 19일
댓글: Stephen23 2021년 7월 28일
I have a vector of size 1*152.Now i want to find the next prime number of every number present in the vector..
Ex: My vector is a=[2 4 7 8] i want the output as [2 5 7 11]..i.e., if the number is a prime then that number will be the output i.e., like 2 and 7 in the given example...
I tried using nextprime like below it gives the following error:
case 1:
>>nextprime(sym(100))
Undefined function 'nextprime' for input arguments of type 'sym'.
case 2:
>> nextprime(3)
Undefined function 'nextprime' for input arguments of type 'double'.

채택된 답변

Basil C.
Basil C. 2018년 2월 19일
Method 1 This functionality does not run in MATLAB and can be used only via MuPAD Notebook Interface.
  • To create an MuPAD interface use the following code
mupad
nb = allMuPADNotebooks
Then a interface screen shall pop up where you can proceed by using the nextprime(num) function.
Method 2
  • You could also create a user defined function to compute the next prime number. This function takes only a non-negative integers as an argument
function p = nextprime(n)
if (isprime(n))
p=n;
else
while(~isprime(n))
n=n+1;
end
p=n;
end
end
  댓글 수: 4
Walter Roberson
Walter Roberson 2019년 12월 21일
No. Consider nextprime(3) . isprime(3) is true, so your code would return the non-prime 4.
function Q = nextprime(n)
if (isprime(n))
n=n+1;
end
while(~isprime(n))
n=n+1;
end
Q=n;
end
However, this can be simplified down to Stephen's code of always adding 1 to n first
Hicham Satti
Hicham Satti 2020년 8월 31일
%That will run very well
function k=next_prime(n)
if ~n>0 || n~=fix(n) || ~isscalar(n)
error ('Tap a integer positif scalar argument');
else
if isprime(n+1)
k=n+1;
else
j=n+1;
while ~isprime(j)
k=j+1;
j=j+1;
end
end
end

댓글을 달려면 로그인하십시오.

추가 답변 (12개)

Arafat Roney
Arafat Roney 2020년 5월 11일
function k=next_prime(n)
i=n+1;
if(isprime(i))
k=i;
else
while(~isprime(i))
i=i+1;
end
k=i;
end
end
  댓글 수: 3
THIERNO AMADOU MOUCTAR BALDE
THIERNO AMADOU MOUCTAR BALDE 2020년 12월 19일
thanks
Kartik rao
Kartik rao 2021년 4월 21일
thanks

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2018년 2월 19일
nextprime() was added to the Symbolic Toolbox in R2016b.
In releases before that,
feval(symengine, 'nextprime', sym(100))

Siddharth Joshi
Siddharth Joshi 2020년 4월 25일
function k = next_prime(n)
if (~isscalar(n) || n<1 || n ~= fix(n))
error('n should be positive scalar ineger!!!')
else
p=-1;
while p<=0
n=n+1;
p=isprime(n)
end
k=n
end
end
k = next_prime(79)
k =
83
  댓글 수: 4
Walter Roberson
Walter Roberson 2021년 7월 28일
isprime() returns 0 (false) or 1 (true). Comparing that as < 0 is going to be false except the first time due to the initialization of p=-1 .
The code would have been better as
function k = next_prime(n)
if (~isscalar(n) || n<1 || n ~= fix(n))
error('n should be positive scalar ineger!!!')
else
p=false;
while ~p
n=n+1;
p=isprime(n);
end
k=n;
end
end
Stephen23
Stephen23 2021년 7월 28일
"The code would have been better as"
... and ultimately simplifies down to this.

댓글을 달려면 로그인하십시오.


Buwaneka Dissanayake
Buwaneka Dissanayake 2020년 6월 21일
% what's wrong with this? it take too long to run & fail
function n = next_prime(n)
k = n+1;
while ~isprime(k)
n = n+1;
end
end
what's wrong with this? it take too long to run & fail.
  댓글 수: 2
Walter Roberson
Walter Roberson 2020년 6월 21일
you test if k is prime but you increment n
SAKSHI CHANDRA
SAKSHI CHANDRA 2020년 7월 22일
your control statement defines k but there is nothing related in the block statements which would check
~isprime(k)

댓글을 달려면 로그인하십시오.


MD SADIQUE IQBAL
MD SADIQUE IQBAL 2020년 7월 17일
unction n = next_prime(n)
k = n+1;
while ~isprime(k)
n = n+1;
end
end
  댓글 수: 2
Stephen23
Stephen23 2020년 7월 17일
Fails every basic test:
>> next_prime(1)
ans = 1
>> next_prime(2)
ans = 2
>> next_prime(3) % infinite loop, stop using ctrl+c
>> next_prime(4)
ans = 4
>> next_prime(5) % infinite loop, stop using ctrl+c
>> next_prime(6)
ans = 6
>> next_prime(7) % infinite loop, stop using ctrl+c
I can see the pattern... it is a very big hint as to what the bug is. As is reading this thread.
SAKSHI CHANDRA
SAKSHI CHANDRA 2020년 7월 22일
your control statement defines k but there is nothing related in the block statements which would check
~isprime(k)

댓글을 달려면 로그인하십시오.


SAKSHI CHANDRA
SAKSHI CHANDRA 2020년 7월 22일
function k = nxt_prime(n)
k=n+1;
while ~isprime(k)
k=k+1;
end
end

Ravindra Pawar
Ravindra Pawar 2020년 8월 13일
편집: Ravindra Pawar 2020년 8월 13일
function k = next_prime(n) %function definition
while ~isprime(n+1) %if n+1 is prime we are out of for loop else loop restarts
n = n+1;
end
k = n+1;
end

shweta s
shweta s 2020년 8월 14일
%to find the next prime no.
function p = next_prime(n)
if (isprime(n))
p=n+1;
else
while(~isprime(n))
n=n+1;
end
p=n;
end
end
  댓글 수: 3
Rik
Rik 2021년 4월 15일
@Sai Krishna Praneeth Duggirala And that is why you should be smart if you want to cheat from this page.
Walter Roberson
Walter Roberson 2021년 4월 15일
Fails for any prime except 2.

댓글을 달려면 로그인하십시오.


Hicham Satti
Hicham Satti 2020년 8월 31일
%That will run very well
function k=next_prime(n)
if ~n>0 || n~=fix(n) || ~isscalar(n)
error ('Tap a integer positif scalar argument');
else
if isprime(n+1)
k=n+1;
else
j=n+1;
while ~isprime(j)
k=j+1;
j=j+1;
end
end
end
  댓글 수: 3
Hicham Satti
Hicham Satti 2020년 9월 8일
why the other codes answers are not deleted ??
Rik
Rik 2020년 9월 8일
Because I'm just one person trying to clean up thread like this. And you didn't answer my question (neither here, nor on the other next_prime thread).

댓글을 달려면 로그인하십시오.


Pragyan Dash
Pragyan Dash 2020년 9월 19일
function k = next_prime(n)
while (~isprime(n + 1))
n = n + 1;
end
k = n + 1;
end

Malgorzata Frydrych
Malgorzata Frydrych 2021년 6월 26일
function k= next_prime(n)
if ~isscalar(n) || n<=0 || mod(n,1)~=0;
error('number should be a positive integer scalar')
end
k=0;
while ~isprime(k)
n=n+1;
k=n;
end
end
  댓글 수: 1
Walter Roberson
Walter Roberson 2021년 6월 26일
Is this efficient? If you are currently at 15, is there a point in testing 16?

댓글을 달려면 로그인하십시오.


Dikshita Madkatte
Dikshita Madkatte 2021년 7월 14일
편집: Dikshita Madkatte 2021년 7월 14일
function k=next_prime(n)
if ~n>0 || n~=fix(n) || ~isscalar(n);
fprintf('n should be positive interger')
end
i=n+1;
if (isprime(i))
k=i;
else
while(~isprime(i))
i=i+1;
end
k=i;
end
end
  댓글 수: 1
Rik
Rik 2021년 7월 14일
A few remarks:
fprintf is not an error. Your code will still run after it fails the check.
You can increment i by two, since 2 is the only even prime, and the while loop will not be reached if n is 1.
You forgot to write documentation for your function. What is this going to teach? Why should it not be deleted?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Number Theory에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by