Hello everyone. I am a Japanese who is not good at English. The first theme I will post in the Wolfram Community is twin prime. Simply enter any positive integer $n$ (= 1,2,3
) to find the twin prime. Of course, use Mathematica. However, it does not use Prime [ $n$] to find twin primes.
The prime and twin prime numbers can be determined by the following four equations. Let the equations be $(1{\rm a})$, $(1{\rm b})$, $(1{\rm c})$, $(1{\rm d})$. Equations $(1{\rm a})$ and $(1{\rm b})$ generate $(6n-1)$ type primes, and equations $(1{\rm c})$ and $(1{\rm d})$ generate $(6n + 1)$ type primes.
Twin primes can be determined by four equations: $(1{\rm a})$, $(1{\rm b})$, $(1{\rm c})$, $(1{\rm d})$. Since this paper focuses on twin primes, we will omit the discussion of prime numbers and only explain twin prime numbers.
$$\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}$$
Give these four equations the same integer $n$. When there is no integer solution for $s$ and $m$, a twin prime is born from the integer $n$.
If $n$ is expressed as $n_t$ when there is no integer solution in $s$ and $m$ in equation $(1)$, the twin prime is given by equation $(2)$ .
$$\begin{equation} {\rm Twin\ prime}= \left \{ \begin{array}{l} 6n_t-1 \\ 6n_t+1 \end{array} \right.\tag{2} \end{equation} $$
$$\begin{equation}n_t: {\rm In\ Equation\ (1),{\it n}\ when\ there\ is\ no\ integer\ solution\ for\ {\it s}\ and\ {\it m}.}\end{equation}$$
Here is a Mathematica program for finding twin primes from Equation $(1)$ and Equation $(2)$. Search for the range from $n$ to $( n +100)$ by giving $n$. Find only one solution. If you want to perform a search with or without a solution in that range, specify $n$ to start next.
n = 1;
Clear[nt];
If[n > 0 && IntegerQ[n],
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];
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[dat1 == {} && dat2 == {} && dat3 == {} && dat4 == {}, nt = n,
nt = "(-_-;) There is no solution in this section."];
If[dat1 == {} && dat2 == {} && dat3 == {} && dat4 == {},
Break[]], {n, n, n + 100}];
Print["====== The calculation results are shown below. ======"];
Print["nt= ", nt];
If[IntegerQ[nt], Print["Twin prime= ", {6*nt - 1, 6*nt + 1}],
Print["Twin prime= (-_-;)There are no twin primes in the \
interval,at (n)~(n+100)."]];
Print["PrimeQ= ", {PrimeQ[6*nt - 1], PrimeQ[6*nt + 1]}];
If[IntegerQ[nt],
Print["If you want to find the next twin prime, start with n= ",
nt + 1],
Print["If you want to continue, start with n= ", n + 101]],
Print["***** ! Please enter a positive integer. *****"]];
Enter any positive integer in the first $n$ of the program. For example, if you enter $n=1$ as in the program above, you will get the following calculation results:
====== The calculation results are shown below. ======
nt= 1
Twin prime= {5,7}
PrimeQ= {True,True}
If you want to find the next twin prime, start with n= 2
If you enter $n = 1$ like this, you get the first twin prime { $5,7$}. PrimeQ checks that the two numbers are indeed prime numbers.
And if you want to find the next twin prime number, specify $n$ to start.
There is something to note here. Equation $(1)$ is the fact that we are working with prime numbers greater than $5$. "$2$" and "$3$" are prime numbers that everyone knows. So ${2,3}$ feels like a twin prime, and ${3,5}$ also looks like a twin prime. Rather than say ${2,3,5}$ is a triplet prime.
Considering the rules for generating prime numbers including "$2$" and "$3$", it is very complicated. This may be that the natural world is annoying us. Except for "$2$" and "$3$", if we consider only prime numbers greater than $5$, we arrive at the simple rule of equation $(1)$.
Let's search for twin primes by entering positive integers in order from $1$ to . The result is shown in the following table.
$$\begin{array}{cc} n_t & \text{Twin prime} \\ 1 & \{5,7\} \\ 2 & \{11,13\} \\ 3 & \{17,19\} \\ 5 & \{29,31\} \\ 7 & \{41,43\} \\ 10 & \{59,61\} \\ 12 & \{71,73\} \\ 17 & \{101,103\} \\ 18 & \{107,109\} \\ 23 & \{137,139\} \\ 25 & \{149,151\} \\ 30 & \{179,181\} \\ 32 & \{191,193\} \\ 33 & \{197,199\} \\ 38 & \{227,229\} \\ 40 & \{239,241\} \\ 45 & \{269,271\} \\ 47 & \{281,283\} \\ 52 & \{311,313\} \\ \text{Omitted below} & \text{Omitted below} \end{array}$$
Please try to enter your favorite integer. (I have attached the file)
Let's enter a slightly larger integer for testing. Try entering $n = 31415926535897932385$ (somehow familiar numbers, yes, I removed the decimal point from $?$).The result is this:
$$\begin{array}{cc} n_t & \text{Twin prime} \\ 31415926535897932462 & \{188495559215387594771,188495559215387594773\} \end{array}$$
Calculation time increases when $n$ increases. On my computer, this example took about 5 seconds. If you are a professional programmer, you might write a bit faster code.
What I felt in the process of creating this program was the surprise that Mathematica's "PrimeQ" makes an instant decision, no matter how large it is.
If my English explanation is difficult to understand, see the program. It may help understanding.