- To create an MuPAD interface use the following code

- 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

79 views (last 30 days)

Show older comments

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.
on 19 Feb 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

Hicham Satti
on 31 Aug 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

Arafat Roney
on 11 May 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

Walter Roberson
on 19 Feb 2018

nextprime() was added to the Symbolic Toolbox in R2016b.

In releases before that,

feval(symengine, 'nextprime', sym(100))

Siddharth Joshi
on 25 Apr 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

Buwaneka Dissanayake
on 21 Jun 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.

SAKSHI CHANDRA
on 22 Jul 2020

your control statement defines k but there is nothing related in the block statements which would check

~isprime(k)

MD SADIQUE IQBAL
on 17 Jul 2020

unction n = next_prime(n)

k = n+1;

while ~isprime(k)

n = n+1;

end

end

SAKSHI CHANDRA
on 22 Jul 2020

your control statement defines k but there is nothing related in the block statements which would check

~isprime(k)

SAKSHI CHANDRA
on 22 Jul 2020

function k = nxt_prime(n)

k=n+1;

while ~isprime(k)

k=k+1;

end

end

Ravindra Pawar
on 13 Aug 2020

Edited: Ravindra Pawar
on 13 Aug 2020

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
on 14 Aug 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

Hicham Satti
on 31 Aug 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

Rik
on 8 Sep 2020

Pragyan Dash
on 19 Sep 2020

function k = next_prime(n)

while (~isprime(n + 1))

n = n + 1;

end

k = n + 1;

end

Malgorzata Frydrych
on 26 Jun 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

Walter Roberson
on 26 Jun 2021

Is this efficient? If you are currently at 15, is there a point in testing 16?

Dikshita Madkatte
on 14 Jul 2021

Edited: Dikshita Madkatte
on 14 Jul 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

Rik
on 14 Jul 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?

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

Start Hunting!