MATLAB: Script doesn’t seem to be execute properly

functioniterationloopsSymbolic Math Toolboxtimewhile loop

I was hoping to acquire some help on how to make my program work efficiently and not take a substantial amount of time to finish:
clear variables
a=...;
p=nextprime(a);
count=0;
limit=200000;
tic
while isprime((p-1)/2)~=1
a=a+1;
p=nextprime(a);
count=count + 1;
if count>limit
break
end
end
toc
This program outputs a number p greater than a such that p is prime and (p-1)/2 is prime. However I've noticed that for any number a greater than approximately 15 digits, the program will take an absurd amount of time to finish, which isn't ideal since I need to test numbers of the order 10^50.

Best Answer

Beyond about 4E15 the distance between adjacent representable doubles becomes greater than 1. p becomes forced to be even (and so not a prime) and p-1 becomes the same as p .
You can do marginally better by switching to uint64, which gets you to about 1.8E19 . But you cannot get beyond that using ordinary numeric forms.
You need to switch to a variable precision toolbox, such as Symbolic Toolbox, or John D'Errico's File Exchange contribution for variable precision integers.