Message Boards Message Boards

3
|
3718 Views
|
0 Replies
|
3 Total Likes
View groups...
Share
Share this post:

Find prime number closest to any integer (lower)

Posted 5 years ago

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:

$$\begin{eqnarray} n&=&6 s_1^2+(6 s_1-1)(m_1-1)\tag{1a} \\ n&=&6 s_2^2+6 s_2+1+(6 s_2+1)(m_2-1)\tag{1b} \\ n&=&6 s_3^2-2 s_3+(6 s_3-1)(m_3-1)\tag{1c}\\ n&=&6 s_4^2+2 s_4+(6 s_4+1)(m_4-1)\tag{1d} \end{eqnarray}$$

$$\begin{equation}n=1,2,3\cdots\end{equation}$$

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$).

$$\begin{eqnarray} (6n-1)\text{ type prime}&=&6n_m-1\tag{2a} \\ (6n+1)\text{ type prime}&=&6n_p+1\tag{2b} \\ \end{eqnarray}$$

$$\begin{equation}n_m\ {\rm and}{\text{ }}n_p: {\rm In\ Equation\ (1),{\it n}\ when\ there\ is\ no\ integer\ solution\ for\ {\it s}\ and\ {\it m}.}\end{equation}$$

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}{\text ^} 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.

$$\begin{array}{ccc} {\rm j\;\, (Any\ integer)} & {\rm prime\ number\ closest\ to\ j\ (lower)}&{\rm difference}\\ 5 & 5 &0\\ 10 & 7&-3 \\ 15 & 13&-2 \\ 20 & 19&-1 \\ 78 & 73&-5 \\ 123 & 113&-10 \\ 567 & 563&-4 \\ 999 & 997&-2 \\ 54321 & 54319&-2 \\ 123456789 & 123456761 &-28\\ 5454545454545454 & 5454545454545449&-5 \\ 9876543210123456789 & 9876543210123456781&-8 \\ 3141592653589793238462643 & 3141592653589793238462637&-6 \\ \text{Omitted below} & \text{Omitted below} &\text{Omitted below} \end{array}$$

POSTED BY: Koichi Ohno
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract