MATLAB Answers

My function is running fine for small no but not for big no.Can anybody explain?

3 views (last 30 days)
Nishant Raj
Nishant Raj on 25 Sep 2021
Edited: Walter Roberson on 25 Sep 2021
function k=next_prime(n)
k=2;
while(k>2)
if(isprime(k)==1)
if(k>n)
return
else
k=k+1;
end
else
k=k+1;
end
end

Accepted Answer

DGM
DGM on 25 Sep 2021
Edited: DGM on 25 Sep 2021
Consider:
a = next_prime(17560)
a = 17569
b = next_prime(5346551)
b = 5346571
function k = next_prime(n)
k = n;
while true
if isprime(k)
break;
end
k=k+1;
end
end
  3 Comments

Sign in to comment.

More Answers (0)

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!