I wrote code where you input a prime, P > 3, and the next prime is the output. However it involves using a recursive formula with the number of recursive steps being in the order of P and using the Mod operator. Thus P must be below 255. How can I avoid this. Follows is my code:(adsbygoogle = window.adsbygoogle || []).push({});

prime = 127 (* can be any prime >3 and < 255 *)

c = prime + 2;

While[True,

d = c-prime + 1;

Clear[g];

g[0] = 0; g[1] = 1; g[x_] := g[x] = Mod[(d g[x-1] - g[x-2]),c];

If[g[c-1]==0,(If[g[c]==1,prime = c;Break[]])];

If[g[c+1]==0,(If[g[c+2]==1,prime = c; Break[]])];

c+=2];prime

**Physics Forums | Science Articles, Homework Help, Discussion**

Join Physics Forums Today!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

# How do I avoid the recursion limit in Mathematica?

**Physics Forums | Science Articles, Homework Help, Discussion**