MATLAB: Function to find the next prime number…

finding the next prime

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

Best Answer

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