Here is a program that searches for a prime number closest to an arbitrary integer. This time is the lower search.
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. Entering any integer in {\rm j} returns the lower prime number closest to {\rm j}. Search from 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, -1}];
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, -1}];
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 >= 0 && pp >= 0, maxp = Max[pm, pp], maxp = Min[pm, pp]];
Print["j = ", j];
If[Or[pm >= 1, pp >= 1],
Print["Prime number closest to j (lower) = " , maxp],
Print["Prime number closest to j (lower)= There is no solution in \
the searched interval."]];
If[Or[nm >= 1, np >= 1], Print["difference = ", maxp - j],
Print["difference = -"]];
If[Or[nm >= 1, np >= 1], Print["PrimeQ = ", PrimeQ[maxp]],
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 (lower) = 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 {\rm difference} = 0. {\rm PrimeQ} confirms that it is 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 (lower) = 26881171418161354484126255515800135873611107
difference = -12
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 Prime\ number\ close\ to\ j} = 26881171418161354484126255515800135873611107\;
Some calculation examples are shown in the table.