I am trying to create a function for finding the immediate next prime number for the given input n. I am trying to run it online and the error is regarding server time out. I have checked my code a gazillion times and unable to grasp the issue. I have tried many different approaches, consulted online forums as well as got my function timed in MATLAB for various outputs even of the order 10^7. It stills displays the same server time out error. I am posting the code here to get comments on it by the teacher/TA.
function k=next_prime(n)if n<2k=1;return;endif n>=2 & n<=1000d=0;while(isprime(d)==0)d=n+1;endk=d;endif (rem(n,2)==0 & n>1000)n=n+1;endif (rem(n,2)~=0 & n>1000)partialsieve = primes(1000);partialsieve(1) = [];while trueif ~all(rem(n,partialsieve))n = n + 2;elseif (isprime(n)==1)k = n;break;elsen = n + 2;endendendendend
Best Answer