- #1
ramsey2879
- 841
- 3
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:
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
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