Function to find the next prime number...

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'.

 Respuesta aceptada

Basil C.
Basil C. el 19 de Feb. de 2018
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 comentarios

Stephen23
Stephen23 el 19 de Feb. de 2018
Editada: Stephen23 el 19 de Feb. de 2018
The if is not required:
function n = nextprime(n)
n=n+1;
while ~isprime(n)
n=n+1;
end
end
>> n = 1;
>> n = nextprime(n)
n = 2
>> n = nextprime(n)
n = 3
>> n = nextprime(n)
n = 5
>> n = nextprime(n)
n = 7
>> n = nextprime(n)
n = 11
>> n = nextprime(n)
n = 13
>> n = nextprime(n)
n = 17
Just a little improvement to the functions written above worked.
function Q = nextprime(n)
if (isprime(n))
Q=n+1;
else
while(~isprime(n))
n=n+1;
end
Q=n;
end
end
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
%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

Iniciar sesión para comentar.

Más respuestas (12)

Arafat Roney
Arafat Roney el 11 de Mayo de 2020
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 comentarios

Walter Roberson
Walter Roberson el 12 de Mayo de 2020
Your if is not needed, you can go directly to the while,
thanks
Kartik rao
Kartik rao el 21 de Abr. de 2021
thanks

Iniciar sesión para comentar.

Walter Roberson
Walter Roberson el 19 de Feb. de 2018
nextprime() was added to the Symbolic Toolbox in R2016b.
In releases before that,
feval(symengine, 'nextprime', sym(100))
Siddharth Joshi
Siddharth Joshi el 25 de Abr. de 2020
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 comentarios

Chaitanya Milampure
Chaitanya Milampure el 28 de Jul. de 2021
might be a stupid doubt, but why does p<0 and p<=0 make a big difference?
Stephen23
Stephen23 el 28 de Jul. de 2021
Editada: Stephen23 el 28 de Jul. de 2021
"might be a stupid doubt, but why does p<0 and p<=0 make a big difference?"
Because the author of this code did not understand how to handle logical data in a simpler way. Instead they obfuscated the simple logical condition behind some numeric comparisons: look at what p value is being used for, and what values it can have.
Once the various bugs and "features" are ironed out, all of these answers boil down to the same concept (which is easy to implement in just a few lines, even if not the most efficient approach to finding prime numbers).
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 el 28 de Jul. de 2021
"The code would have been better as"
... and ultimately simplifies down to this.

Iniciar sesión para comentar.

Buwaneka Dissanayake
Buwaneka Dissanayake el 21 de Jun. de 2020
% 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 comentarios

Walter Roberson
Walter Roberson el 21 de Jun. de 2020
you test if k is prime but you increment n
your control statement defines k but there is nothing related in the block statements which would check
~isprime(k)

Iniciar sesión para comentar.

MD SADIQUE IQBAL
MD SADIQUE IQBAL el 17 de Jul. de 2020

0 votos

unction n = next_prime(n)
k = n+1;
while ~isprime(k)
n = n+1;
end
end

2 comentarios

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 el 22 de Jul. de 2020
your control statement defines k but there is nothing related in the block statements which would check
~isprime(k)

Iniciar sesión para comentar.

SAKSHI CHANDRA
SAKSHI CHANDRA el 22 de Jul. de 2020
function k = nxt_prime(n)
k=n+1;
while ~isprime(k)
k=k+1;
end
end
Ravindra Pawar
Ravindra Pawar el 13 de Ag. de 2020
Editada: Ravindra Pawar el 13 de Ag. de 2020

0 votos

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 el 14 de Ag. de 2020
%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 comentarios

fails when n=3
Rik
Rik el 15 de Abr. de 2021
@Sai Krishna Praneeth Duggirala And that is why you should be smart if you want to cheat from this page.
Walter Roberson
Walter Roberson el 15 de Abr. de 2021
Fails for any prime except 2.

Iniciar sesión para comentar.

Hicham Satti
Hicham Satti el 31 de Ag. de 2020
%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 comentarios

Rik
Rik el 8 de Sept. de 2020
Same question here as with your other answers: why are you posting solutions to homework questions? What does it teach?
Since this exact solution has been posted before in this exact thread I will delete this answer if you don't respond to that question.
Hicham Satti
Hicham Satti el 8 de Sept. de 2020
why the other codes answers are not deleted ??
Rik
Rik el 8 de Sept. de 2020
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).

Iniciar sesión para comentar.

Pragyan Dash
Pragyan Dash el 19 de Sept. de 2020
function k = next_prime(n)
while (~isprime(n + 1))
n = n + 1;
end
k = n + 1;
end
Malgorzata Frydrych
Malgorzata Frydrych el 26 de Jun. de 2021
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 comentario

Walter Roberson
Walter Roberson el 26 de Jun. de 2021
Is this efficient? If you are currently at 15, is there a point in testing 16?

Iniciar sesión para comentar.

Dikshita Madkatte
Dikshita Madkatte el 14 de Jul. de 2021
Editada: Dikshita Madkatte el 14 de Jul. de 2021
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 comentario

Rik
Rik el 14 de Jul. de 2021
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?

Iniciar sesión para comentar.

Preguntada:

el 19 de Feb. de 2018

Comentada:

el 28 de Jul. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by