Here is a program that searches for a prime number closest to an arbitrary integer.? However, there are conditions. The condition is that the specified integer is 5 or more and the search is limited to the upper side.
Prime numbers greater than 5 can be determined by the following four equations:
Equations (1{\rm a}) and (1{\rm b}) determine prime numbers of type (6n-1), and equations (1{\rm c}) and (1{\rm d}) determine prime numbers of type (6n + 1). Given an arbitrary positive integer n in equation (1) and there are no integer solutions in s and m, n determines the prime number. If there is no integer solution for s and m in equations (1{\rm a}) and (1{\rm b}), let n be n_m. Similarly, in equations (1{\rm c}) and (1{\rm d}), n is expressed as n_p when there is no integer solution for s and m. The prime number is determined from n_m and n_p according to equation (2).
Here is a program that uses this equation (1) and (2) to find the nearest prime number to an arbitrarily specified positive integer {\rm j}. There is a relation n=(\;{\rm j}-1)\,/\,6\; between {\rm j} and n. I wrote this in Mathematica as n = {\rm Quotient}\, [(\,{\rm j}-1), 6]\, and converted {\rm j} to n. If any integer is input to {\rm j}, the prime number closest to {\rm j} is output. Search the range of n to (n + 100).
j = 5;
Clear[nm, np, pm, pp, minp];
n = Quotient[(j - 1), 6];
If[j >= 5 && n >= 0 && IntegerQ[j],
Do[{dat1 =
FindInstance[
6*s1^2 + (6*s1 - 1)*(m1 - 1) == n && 0 < s1 && 0 < m1, {s1, m1},
Integers];
dat2 =
FindInstance[
6*s2^2 + 6*s2 + 1 + (6*s2 + 1)*(m2 - 1) == n && 0 < s2 &&
0 < m2, {s2, m2}, Integers]};
If[dat1 == {} && dat2 == {}, nm = n, nm = 0];
If[dat1 == {} && dat2 == {} && ((6*nm - 1) - j) >= 0, Break[]], {n,
n, n + 100}];
Do[{dat3 =
FindInstance[
6*s3^2 - 2*s3 + (6*s3 - 1)*(m3 - 1) == n && 0 < s3 &&
0 < m3, {s3, m3}, Integers];
dat4 =
FindInstance[
6*s4^2 + 2*s4 + (6*s4 + 1)*(m4 - 1) == n && 0 < s4 &&
0 < m4, {s4, m4}, Integers]};
If[dat3 == {} && dat4 == {}, np = n, np = 0];
If[dat3 == {} && dat4 == {} && ((6*np + 1) - j) >= 0, Break[]], {n,
n, n + 100}];
Print["====== The calculation results are shown below. ======"];
If[nm > 0, pm = 6*nm - 1, pm = 0];
If[np > 0, pp = 6*np + 1, pp = 0];
If[pm >= 1 && pp >= 1, minp = Min[pm, pp], minp = Max[pm, pp]];
Print["j = ", j];
If[Or[pm >= 1, pp >= 1],
Print["Prime number closest to j (upper) = " , minp],
Print["Prime number closest to j (upper)= There is no solution in \
the searched interval."]];
If[Or[nm >= 1, np >= 1], Print["difference = ", minp - j],
Print["difference = -"]];
If[Or[nm >= 1, np >= 1], Print["PrimeQ = ", PrimeQ[minp]],
Print["PrimeQ = -"]];
If[Or[pm >= 1, pp >= 1], Print["Enter j for the next search. "],
Print["There is no solution for this interval. If you want to \
continue, start with the next j. j = ", (n + 101)*6]],
Print["***** ! Please enter an integer greater than 5 *****"]];
The calculation result is shown as follows. This is an example when {\rm j} = 5.
====== The calculation results are shown below. ======
j = 5
Prime number closest to j (upper) = 5
difference = 0
PrimeQ = True
Enter j for the next search.
The closest prime number for {\rm j} = 5 is 5, so the difference is difference = 0. {\rm PrimeQ} confirms that it is a prime number.
Let’s put a little bigger number. Let’s assume {\rm j} = 78. Does “78” have any meaning? Nothing in particular. This is my age. The calculation result is as follows.
====== The calculation results are shown below. ======
j = 78
Prime number closest to j (upper) = 79
difference = 1
PrimeQ = True
Enter j for the next search.
The closest prime number to {\rm j} = 78 is “79”. (Next year my age will be a prime number)
Let’s check a slightly larger number.
Try:
{\rm j}\,=\,26881171418161354484126255515800135873611119
This number is {\rm{Round}}\, [{\rm{E}}^{100}]. The calculation result is as follows.
====== The calculation results are shown below. ======
j = 26881171418161354484126255515800135873611119
Prime number closest to j (upper) = 26881171418161354484126255515800135873611287
difference = 168
PrimeQ = True
Enter j for the next search.
The input integer {\rm j} and the nearest prime number are as follows.
{\rm j}\,=\,26881171418161354484126255515800135873611119
{\rm A\ prime\ number\ close\ to\ j}\,=\,26881171418161354484126255515800135873611287
Some calculation examples are shown in the table.