text:
\begin{document}
\maketitle
\begin{abstract}
Modern cryptography algorithms are commonly used to ensure information security. Prime numbers are needed in many asymmetric cryptography algorithms. For example, RSA algorithm selects two large prime numbers and multiplies to each other to obtain a large composite number whose factorization is very difficult. Producing a prime number is not an easy task as they are not distributed regularly through integers. Primality testing algorithms are used to determine whether a particular number is prime or composite. In this paper, an intensive survey is thoroughly conducted among the several primality testing algorithms showing the pros and cons, the time complexity, and a brief summary of each algorithm. Besides, an implementation of these algorithms is accomplished using Java and Python as programming languages to evaluate the efficiency of both the algorithms and the programming languages.
\end{abstract}

\section{Introduction}
Cryptography, Greek work, refers to “secret writing” or the encryption and decryption of a message using a secret key. In computer science, cryptography is defined as the art of transforming messages to make them secure and immune to attacks. Currently, the commonly used cryptographic systems are: symmetric key , public key, and hashing. These play major roles in information security~\cite{wang2017lightweight}. Security keys are the basic component of many cryptography algorithms and, as the processing speed of computers increases, longer keys are required to ensure a high level of security. Generating such keys essentially depends on arithmetic computations~\cite{diffie1976new}.

Positive integers larger than one are of two types: composite integers which can be written or factorized as a multiplication of two or more integers larger than one. The second type are primes (or prime integers) which have exactly two factors; one and the prime number itself. Primes do not follow a pattern of distribution on the number line; they appear chaotically. The study of prime integers and their characteristics began around 300 B.C in the Greek city of Alexandria which is now Egypt~\cite{yan2009primality}. Since then, there were no significant findings until the fifteen century when Fermat’s little theorem~\cite{daepp2011fermat} appeared and made several contributions to the theory of primes. Fermat’s little theorem has surprising consequences for encryption and primality testing.  So far, the largest prime number discovered is ($2^{82,589,933}-1$)~\cite{Mersene2018}.

Prime numbers are ubiquitous in modern cryptography. Indeed, prime numbers are fundamental in the keys generation of public-key cryptosystem algorithms. For example, the RSA (Rivest-Shamir-Adelman) algorithm~\cite{rivest1978method} selects two very large prime numbers and then multiplies them to generate a large composite number which is very difficult to factorize. Because prime numbers are distributed evenly across the number line, generating them can be very difficult. Filtering primes from composites is easier if the integers range is limited, however, as the range expands, this task becomes more challenging and time consuming. A solution to this problem is to select either a true or pseudo-random and test the primality of this number by passing it through one of the available primality tests. Accuracy and speed are two important factors in any primality testing algorithm. Although, deterministic algorithms guarantee 100\% accuracy, they usually suffer a large computational overhead. On the other hand, randomized or probabilistic algorithms are usually faster, but these algorithms do not guarantee whether the input number is prime or composite and as such, a small error probability should be considered.

In this paper, we intensively survey and classify different primality testing algorithms mentioning the advantages, disadvantages, and time complexity of each algorithm, Moreover, for the sake of real evaluation, implementation of these primality testing algorithms is done using both Java and Python programming languages.

The rest of the paper is organized as follows: section~\ref{section: Background and Terminology} introduces a brief background and terminology, while section~\ref{section: RelatedWork} outlines the literature review. In section~\ref{section:Taxonomy}, the taxonomy and survey of primality testing algorithms is explained. A practical evaluation using Java and Python, along with the results are provided in section~\ref{section: Practical} and the paper is concluded in section~\ref{section:Conclusion}.

\section{Background}
\label{section: Background and Terminology}

Prime numbers are the atoms of number theory in the sense that they cannot be decomposed into products of smaller numbers and in the sense that all numbers are the product of prime numbers which is formulated by the following theorem.

Theorem~\cite{stein2008elementary}: if $n$ is a composite integer, then there is a prime number $p$ which divides $n$, such that $p \leq \sqrt{n}$.

From the number theory, numbers that are used for primality testing can be classified into two groups: generic numbers and numbers with special characteristics. Next, we provide some important related definitions.

\emph{Mersenne number}~\cite{robinson1954mersenne} is a number in the form ($2^n-1$), where n is a prime number. If such a number is prime, it is called Mersenne primes. Examples of Mersenne primes are M1, M3, M5, M4423. Not all such numbers are primes, e.g., M=11 gives $(2^{11}  -1) =2047=23*89$.  the largest prime number discovered so far is in the form of Mersenne ($2^{82,589,933}-1$)~\cite{Mersene2018}.

\emph{Fermat number}~\cite{robinson1954mersenne} is a number in the form ($2^m+1$). If this number is a prime, then $m$ has the form $m=2^n$. Fermat conjectured that all these numbers are primes, however it turned out that this conjecture is incorrect. Euler showed that for $m=5$ the formula gives a composite number. There are three important facts about Fermat numbers which are: (1) only the first four Fermat numbers are known to be prime: 5, 17, 257, and 65537; (2) the next 243 numbers are known to be composite; and (3) recently, it has been conjectured that only the first four Fermat numbers are prime.

\emph{Proth number}~\cite{helfgott2013numerical} is a number in the form ($k*2^n+1$), where $k$ is an odd positive integer and $n$ is any positive integer such that $2^n > k$.

\emph{Legendre symbol}~\cite{burton2006elementary} is a function of two integers $a$ and $p$ such that $a>0$, and $p$ is a prime number. It is written as $(a/p)$ and defined to be 0 if $p$ divides $a$, +1 if  $a$ is a quadratic residue modulo $p$ , and -1 if $a$ is a quadratic nonresidue modulo $p$. On the other hand, \emph{Jacobi symbol}~\cite{burton2006elementary} is defined as shown in~\ref{eq1:Jacobi}. Legendre symbol could be easily calculated whenever factorization is not a concern. However, in the case of large integers where factorization presents difficulties, Jacobi symbol is useful and efficient since it could be computed without knowing the factorization of either $a$ or $n$.
\begin{equation}
\left( \frac{a}{n} \right)=\prod_{i=1}^{k} \left(\frac{a}{p_i} \right)^{e_i}
\label{eq1:Jacobi}
\end{equation} where $n$ is an odd positive integer and $\left(\frac{a}{p_i} \right)$ denotes the previously defined Legendre symbol.

In 230 B.C. the Sieve method~\cite{miller1928so} was proposed by Eratosthenes, as the first algorithm to generate all prime numbers between 1 to n (also known as the sieve of Eratosthenes). This method can be summarized in the following steps:
\begin{enumerate}
    \item 	Write down all numbers from 2 to $n$. Cross out all multiples of 2 except 2 itself. In this case all even integers up to $n$ will be crossed out.
    \item Take the first uncrossed number, say $r$ and cross all multiples of $r$ except $r$ itself.
    \item Repeat step 2 until no number can be crossed out.
    \item The remaining uncrossed numbers are primes.
\end{enumerate}
Eratosthenes realized that there is no need to continue in this way up to $n$ as once a prime number larger than $\sqrt{n}$ is found, then all the remaining odd integers that have not been crossed off must be primes. One limitation of this method is that if $n$ is a huge number, then it suffers a lot of computational and memory overhead since the algorithm takes $O(n^2)$ operations.

\section{Related Work} \label{section: RelatedWork}
In this section, we list the various methods used to evaluate the primality testing algorithms.

L.Monier~\cite{monier1980evaluation} conducts an efficiency comparison between two probabilistic primality tests which are Miller-Rabin and Solovay-Strassen. The comparison is based solely on a mathematical model. The model shows that Millar-Rabin is more efficient and accurate than Solovay-Strassen.

R.Schoof~\cite{schoof2008four}  provides a very small survey about four primality testing algorithms : the elliptic curve primality test, Miller-Rabin test, AKS, and the cyclotomic test. The author provided summarized details about the construction of these algorithms, their efficiency and their usage in practice.

R.Canfield~\cite{canfield2008three} discusses three primality tests: Solovay-Strassen, Miller test, and AKS. The most important feature of this work that distinguishes it from the aforementioned works is that a practical work is provided. Implementation is accomplished using Maple code to count the number of steps executed for numbers of various sizes,4 to 12 digits.

M.Kida~\cite{kida2004primality} revisits three original primality tests: Fermat, Miller-Rabin, and Pocklington, and proposes the variation of those primality tests based on groups scheme  over a finite field.

P.Monica~\cite{perrenoud2009randomized} presents some randomized and deterministic algorithms along with some implementations of them in C++. The discussed algorithms are: the naïve algorithm, Miller-Rabin, Fermat, Solovay-Strassen and AKS. The author provides a small survey about primality tests, however, they focused on AKS test and tried to evaluate the efficiency of it compared to Fermat’s test.

C.Duta et al.~\cite{duta2015framework} propose a framework for the evaluation and comparison of 14 Primality testing algorithms. C\# is used as a programming language. However, the primality testing algorithms are not well classified, and some algorithms were used incorrectly to decide about primality, e.g. Lucas-Lehmer deciding about Fermat number!.

Y. Kasarabada~\cite{kasarabada2016verilog,purdy2017hardware} conducts a comparison between Java and Python implementations for only one primality test which is Baillie-PSW test.

None of the aforementioned studies appropriately classifies the primality testing algorithms, neither they do provide a comprehensive implementation or comparisons. In this paper, a concise, comprehensive and novel  classification is presented. Besides, an implementation of 11 primality testing algorithm is accomplished to evaluate the efficiency of both the algorithms and the programming languages.

\section{Taxonomy of Primality Testing Algorithms}
\label{section:Taxonomy}
Primality testing algorithms can be generally classified into two categories: deterministic and randomized. However, after extensive search, it turns out that these algorithms should be classified into four categories: 1) Randomized Monto-Carlo  primality tests, 2) randomized Las-Vegas primality tests, 3) Deterministic primality tests, and 4) Heuristic primality tests. Details of these primality tests are provided in the following subsections.

\subsection{Monto-Carlo Randomized Primality Tests}
In this category, an algorithm tests a large integer for primality. If the large number under question is found to be a prime then a percent of error, which depends on the certainty value, should be considered and this number is called a probable prime. Whereas, if it is found to be a composite, then the finding is said to be 100\% accurate and therefore Monto-Carlo randomized tests are more properly called compositeness tests. In this subsection, three primality tests are discussed.

\subsubsection{Fermat Primality Test}
Fermat, or the peusdoprime primality test~\cite{nagell1951fermat}, indicates that if $p$ is a prime number, then for any integer $a$ such that $1 < a < p-1$,~\ref{eq2:Fermat} holds.

\begin{equation}
 a^{p-1} \equiv 1\ (mod\ p).
\label{eq2:Fermat}
\end{equation}
To test the primality of a number $(n)$, a random number $a$ is chosen between $(1,n)$ and then a check is made to see if~\ref{eq2:Fermat} holds. If the chosen $a$ does not satisfy the equality, then a is called a Fermat witness, and $n$ is composite. if many distinguished values of $a$ satisfy the equality, then $n$ is probably prime. However, if $n$ is an odd composite integer and $a$ satisfies the equality, then $a$ is called a Fermat liar (to primality for $n$), and $n$ is called a pseudoprime~\cite{andersen2007pseudoprime}.

\emph{Carmichael numbers}~\cite{carmichael1912composite} are composite integers that always declared by the Fermat test as primes. Namely, a Carmichael number always passes the condition of the Fermat test for any base integer $a$. W. Alford~\cite{alford1994there} proves that there is an infinite quantity of Carmichael numbers.

 Fermat test is considered  a fast primality test, especially if the input number is composite. The main limitations of this algorithm are: 1) The probability of failure for Carmichael numbers is 1 and 2) it cannot determine the number of Fermat liars for a composite number (pseudoprime). The Time complexity of this test is $O(k \ logn)^3$.
\newline
\newline
\subsubsection{Solovay-Strassen Primality Test}

This test was proposed by Robert Solovay and Volker Strassen in 1977~\cite{solovay1977fast} to check whether a given number $n$ is a composite or probably prime. It was popular at the beginning of public-key cryptography use, especially within RSA cryptosystem. Solovay-Strassen theorem states: based on Euler theorem~\cite{nagell1951fermat}, if $p$ is a prime number then for any integer $a$, then ~\ref{eq3:solovay} holds.
\begin{equation}
a^{(p-1)/2} \equiv (a/p)\ mod\ p.
\label{eq3:solovay}
\end{equation}
Where $(a/p)$ is called the Legendre symbol. Therefore, to test the primality of $n$, pick distinguish values for ($a$) and test the congruence. If it’s found that any $a$ does not fit the congruence, then $n$ will be a composite, otherwise, $n$ is probably prime.

The main feature of this test is that no pseudoprime number can pass this test. Yet, Euler pseudoprimes numbers, which are odd composite numbers, pass the Solovay-Strassen test, and as such this is considered as a drawback of this test. However, Euler pseudoprimes are less than pseudoprimes. Also, the calculation of Legendre symbol adds more computation overhead which consider as another drawback. The time complexity of this test is $O(k\ logn)^3$.

\subsubsection{Miller-Rabin Primality Test}

The so-called Miller–Rabin, also known as strong pseudoprime primality test, was devised in 1976 by Gary Miller to check whether a given number $n$ is a prime. The first version was deterministic~\cite{miller1976riemann} assuming the correctness of extended Riemann hypothesis (ERH)~\cite{miller1976riemann}. Then Michael Rabin revised it to be a probabilistic algorithm ~\cite{rabin1980probabilistic}. The test detects the primality of a number by testing two criteria:  first, recalls Fermat test and if the input number $n$ is declared as a composite, the test exits early. Second, the test detects a non-trivial square root of  $1 (mod n)$. If there is one, then $n$ is a composite. Otherwise, $n$ is probable prime. The steps below summarize the Miller’s test procedure:
\begin{enumerate}
    \item Suppose $a^{n-1} \equiv 1\ mod\ n$ for all $(a,n)=1$.
    \item If $n$ is prime, then $a^{\frac{(n-1)}{2}} \equiv \pm1(mod\ n)$.
    \item If $n$ is an odd integer and $n-1=2^st$ with t odd, then we say that $n$ passes Miller’s test for the base $a$ iff $a^t\equiv 1(mod\ n)$  or $a^{2jt} \equiv -1(mod\ n)$ for all $j \in \{0,1,…,s-1\}$.
    \end{enumerate}

	This test is used in many practical cryptography systems because of its efficiency. The output of this algorithm is a proof that the number is a composite or  the number is probable prime. Miller-Rabin is a polynomial-time algorithm with a time complexity of $O(k \ logn)^3$. The probability of declaring a composite number as a prime is less than $(1/4)^k$, where $k$ is the certainty value and it ranges between 1 to $n-1$. Composite numbers that pass Miller-Rabin test are called strong-pseudoprimes and they are much fewer than the pseudoprimes. Furthermore, all Carmichael numbers are catched and declared as composite.

\subsection{Las-Vegas Randomized Primality Tests}
In contrast to the Monto-Carlo primality tests, which may return a false positive, this type of tests always declares an input number $n$ as a prime with 100\% certainty but with a running time that varies randomly. One evident or one witness is enough to announce primality. Therefore, these tests are often called \emph{primality proving tests}.
\subsubsection{Proth Test}
The Proth primality test~\cite{proth1878serie} was proposed by François Proth in 1878 to determine whether a given Proth number $n$ is prime . Proth Theorem states: if $p$ is a Proth number in the form $k*2^n  + 1$, where  $k$ is an odd integer such that $k < 2^n$, then $p$ is a prime if~\ref{eq4:proth} holds. At this case $p$ is called a Proth prime. An integer $a$ that satisfies the condition of the theorem can be found by iterating $k$ times and calculating Jacobi symbol $(a/n)$, until  $(a/n)= -1$.
\begin{equation}
a^{(p-1)/2} \equiv -1\ mod\ p.
\label{eq4:proth}
\end{equation}
It was approved that half of the possible values of $a$ satisfy $(a/n)= -1$, if $p$ is prime~\cite{grau2015primality}. Once such $a$ is found, the test declares $n$ as definitely prime and exits. If $a$ completely covers the range $[2,n-1]$ and no such $a$ is found, then $n$ is absolutely composite. The largest ever Proth prime number is $10223*2^{31172165}+1$, which was discovered in 2016~\cite{Proth2016Largest}. The time complexity of Proth test is $O((k\ logk+logn)\ logn)$.

\subsubsection{Lucas Test }
This Las-Vegas Lucas primality test is one of four tests proposed by Édouard Lucas to check whether a given number $n$ is prime~\cite{baillie1980lucas}. Lucas test is considered as a very efficient method for proving primality for moderately sized primes.

Lucas Theorem states: let $n$ be an odd positive integer; if there exists an integer $a$ such that $1 < a < n$ and~\ref{eq5:lucas1} holds, and if all prime factors $q_1,q_2,…,q_s$ of $n-1$ yield~\ref{eq6:lucas2} always holds, then $n$ is a prime. If no such number $a$ exists, then $n$ is composite.
\begin{equation}
a^{n-1} \equiv 1\ mod\ p.
\label{eq5:lucas1}
\end{equation}
\begin{equation}
a^{(n-1)/q_i} \not\equiv -1\ mod\ p.
\label{eq6:lucas2}
\end{equation}
So, in this test the witnesses of primality are the prime factors of $n-1$. Lucas test can work efficiently with any generic number including Mersenne and Fermat. One apparent drawback of this test is that it requires prime factors of $n - 1$ to be already known.

\subsubsection{Pocklington Test}
The Pocklington primality test~\cite{pocklington1914determination} is one of the first randomized primality test algorithms devised in 1914 by Henry Cabourn.

Pocklington Theorem states: let $n \geq 3$ be an odd integer, and suppose that there exist numbers $a$, $1 < a < n$, and $q$ is prime such that $q|(n-1)$ and $q > \sqrt{n}-1$. Then $n$ is prime if the following two conditions are fulfilled.
\begin{enumerate}
    \item $a^{n-1} \equiv 1\ mod\ n$.
    \item $gcd(a^{(n-1)/q}-1,\ n)=1$.
\end{enumerate}
If $a$ fails to satisfy condition (1), then $n$ is not prime. However, if $a$ fails to satisfy condition (2), then $n$ is still under question of primality and a new random $a$ is chosen. If $a$ satisfies the two conditions, then $n$ is certainly prime. Otherwise, if $a$ satisfies condition (1) but not (2), the test declares $n$ as probable composite and exits.

\subsection{Deterministic Primality Tests}
These algorithms provide accurate decision about the input number, whether it is prime or composite. However, generally these algorithms are impractical because of the huge computations required for very large numbers.

\subsubsection{Trivial Division (Naïve) Test}
This is the simplest deterministic primality test~\cite{yan2009primality}. For an input number $n$, this test investigates if any integer $m$, from 2 to $(n-1)$, can divide $n$ evenly (there is no remainder). If $n$ is divided evenly by any $m$, the number is a composite, otherwise it is prime. This test always returns a true answer. However, for large numbers, the algorithm is too slow. Trial division takes $O(\sqrt{n})$ operations to check if an $n$-bit number is prime, which is exponential in the size of the input.

\subsubsection{Pepin Test}
Pepin primality test~\cite{Pepin1877test} is based on Fermat test. In 1877, Pepin proved a method for testing the primality of Fermat numbers. The output of this algorithm is a proof that the number is prime or its primality could not be proven.

Pepin Theorem states that if $F_n$ is a Fermat number where $n$ is greater than zero such that  $F_n  =2^{2^n}+1$ is the $n$th Fermat number, then  $F_n$ is prime, iff~\ref{eq7:Pepin} holds.
\begin{equation}
3^{(F_n-1)/2)}   \equiv 1\ mod\ F_n..
\label{eq7:Pepin}
\end{equation}
Other bases may be used in place of 3, for example 5 or 7~\cite{vsolcova2006fermat}. The time complexity of this algorithm is $O(logn)^2$.

\subsubsection{Lucas-Lehmer Test}
This test was initially developed by Édouard Lucas in 1856~\cite{lehmer1927tests} and then improved by Lucas in 1878 and Derrick Henry Lehmer in the 1930s.

Lucas-Lehmer Theorem states: let $M_p  = 2^p-1$ be a Mersenne number to be tested where $p$ an odd prime. The primality of $p$ can be thoroughly checked with a simple algorithm like trial division since $p$ is exponentially smaller than $M_p$. Then  $M_p$ can be tested for primality by the following steps:
\begin{enumerate}
    \item 	Define variable $s = 4$.
    \item   Find $M^n$  = $2^n -1$.
    \item   $s = ((s * s) - 2) mod M$
    \item  Repeat the third step  $(n - 2)$ times.
\end{enumerate}If the returned value of $s$ is equal to zero, then $n$ is prime, otherwise, $n$  is composite.

\subsubsection{AKS Test}
On August 6th, 2002, M.Agrawal and his two students proposed a deterministic test~\cite{agrawal2004primes}, that runs in polynomial time, for determining if an integer number is prime. The test known as the Agrawal-Kayal-Saxena (AKS) primality test. The test is considered as distinguished and exceptional. Its exceptionality stems from its ability to prove the primality of any given integer in deterministic, polynomial. The idea of AKS is based on generalization of Fermat’s little theorem to polynomials with coefficients in $Z/Z_n$ where $n$  is any prime number. Initially, the complexity of this test was $O(logn^11.5)$~\cite{bedodi2010primality}, but a lot of improvements have been made from 2002 to this time, and as a result the complexity has become  $O(logn^6)$~\cite{bedodi2010primality}.

\subsection{Heuristics Primality Tests}
In this type of primality tests, no mathematical proof or correction is provided. The idea behind these tests is proposed heuristically, e.g., by mixing two tests.
\subsubsection{Baillie-PSW Test}
The test was developed in the 1980’s as a heuristic test that mixes Miller-Rabin test to the base 2 with Lucas-Sequence test ~\cite{weisstein2004baillie, nicely2005baillie}. For an input integer $n$ the test follows the following steps:  initially, the test verifies if $n$ has small prime factors (smaller than 1000). Secondly, the test applies Miller-Rabin test using base $a=2$. Finally, the input $n$ should be tested using the Lucas-Sequence test. If the input $n$ passes the aforementioned tests, then $n$ is a probable prime or a BPSW pseudoprime. On the other hand, if $n$ fails to pass any of the tests, then $n$ is a composite.
This test is considered to be very efficient, since no composite number less than $2^{64}$ has passed it (no BPSW pseudoprimes)~\cite{pomerance1984there}.

Table~\ref{table2:theorComp} summarizes the taxonomy of the primality tests and compares their characteristics, pros, cons, time complexity, and probability of error.

\section{Implementation and experimental Evaluation} \label{section: Practical}
To evaluate the real performance of primality testing algorithms, this section covers the implementation of eleven of those algorithms. Both Java and Python implementations are provided. We try as much as possible to use the built-in language’s functions. For our experiments, we use a PC with an Intel Core i5-2430M Processor, CPU 2.40 GHz, 4 GB RAM. Windows-10 64-bit is used as an operating system. Java JDK 8 and Python 3.6 are used as programming languages.
\begin{table}[H]
\caption{Comprehensive Comparison between different primality tests.}
\label{table2:theorComp}
\centering

\begin{tabular}{|m{0.10\linewidth}|m{0.15\linewidth}|m{0.25\linewidth}|m{0.25\linewidth}|m{0.10\linewidth}|m{0.14\linewidth}|}
\toprule
\textbf{Reference} &	\textbf{Characteristics} &	\textbf{Pros} &	\textbf{cons} &	\textbf{Time Complexity} &	\textbf{Probability of error} \\
\midrule
Fermat~\cite{nagell1951fermat} &
- Randomized (Monto -Carlo).\newline
- Compositeness test. &
- Very simple to implement.\newline
- Base for many tests.&
-Failure probability may reach 1.\newline
-Pseudoprime can pass the test. &
 $O(k\ logn)^3$ &
- For Carmicheal numbers =1.\newline
- For pseudoprimes $\approx245 *10^{-6}$.\\
\midrule
 Solovay\newline Strassen~\cite{solovay1977fast} &
- Randomized (Monto-Carlo).\newline
- Compositeness test.&
- Pseudoprimes are successfully announced as composites.&
- an Euler pseudoprime can pass the test.\newline
- Computation of Jacobi symbol adds more computation overhead.&
$O(k\ logn)^3$ &
less than $\left(\frac{1}{2}\right)^k$, where $k$ is the certainty value.\\
\midrule
Miller-Rabin\newline ~\cite{miller1976riemann,miller1976riemann}&
- Randomized (Monto-Carlo).\newline
- Compositeness test.&
- Fast \& efficient.\newline
- Euler Pseudoprimes are successfully announced as composites.&
- Strong pseudoprimes can pass the test.&
$O(k\ logn)^3$ &
less than $\left(\frac{1}{2}\right)^k$, where $k$ is the certainty value.\\
\midrule
Lucas~\cite{baillie1980lucas}&
- Randomized (Las-Vegas).\newline
- Approving primality test.&
- Valid for any generic, or special form numbers.&
- Prime factors of $n-1$ are required to be already known.\newline
- Worst case scenario may take long time. (if $n$ is composite, this test may not terminate).&
$O(p^2\ log_pn)$ &
- 0 if the number is announced as a prime.\\
\midrule
Proth~\cite{proth1878serie} &
- Randomized (Las-Vegas).\newline
- Approving primality test.&
- Very fast and reliable test to decide about proth number.&
- Working well only with proth numbers.&
$O((k\ logk+logn)logn))$ &
- 0 if the number is announced as a prime.\newline
- $\approx0$ if the base $a$ covers 50\% of the range $[2,n-1]$.
\\
\midrule
Pocklington\newline \cite{pocklington1914determination} &
- Randomized (Las-Vegas).\newline
- Approving primality test.&
- Very efficient if there is a factor $q> \sqrt{n-1}$.&
- Prime factors of $n-1$ are required to be already known.&
$O(lnln\ n)$ &
- 0 if the number is announced as a prime.
\\
\midrule
Lucas\newline Lehmer~\cite{lehmer1927tests} &
- Deterministic.\newline
- Compositeness \& Approving primality test.&
- Testing Mersenne numbers accurately.\newline
- Practical test to find massive primes.&
- Suffers slowness when numbers become huge.\newline
- Prime factors of n-1 are required to be already known.&
$O(n^2\ logn\newline loglogn)$ &
- 0.
\\
\midrule
Trivial Divison~\cite{yan2009primality} &
- Deterministic.\newline
- Compositeness \& Approving primality test.&
- Reliable and simple test for numbers consist of 10 digits or less.&
- Computational infeasible for large numbers (exponential increasing).&
$O(\sqrt{n})$ &
- 0.
\\
\midrule
Pepin~\cite{Pepin1877test}&
- Deterministic.\newline
- Compositeness \& Approving primality test.&
- Efficient test for Fermat form numbers.&
- It is computational infeasible for large Fermat numbers.\newline
- Since the base $a$ is fixed, it works poorly with other formats.&
$O(logn)^2$ &
- 0.
\\
\midrule
AKS ~\cite{agrawal2004primes} &
- Deterministic.\newline
- Compositeness \& Approving primality test.&
- The only reliable deterministic test that works in polynomial time .&
- Slow for small and moderate numbers.&
$O(logn^6)$ &
- 0.
\\
\midrule
Ballie-PSW~\newline \cite{weisstein2004baillie, nicely2005baillie} &
- Huerestic.&
- None of the pseudoprime classes passes this test.&
- No Mathematical approving.\newline
- Considered as compositeness test.&
$O(k\ logn)^3$ &
- 0 for any $n<2^{64}$.
\\
\bottomrule
\end{tabular}
\end{table}
Scenarios to compare efficiency of the programming languages and evaluate the primality tests are provided. Each individual experiment is repeated 5 times and the average is calculated. Mainly, we aim to prove the concepts that are mentioned in the previous section and to compare the built-in functions of Java and Python. For the sake of simplicity and without the loss of generality, the maximum number of digits for any input number we used is 20-digits. Yet, Appendix~\ref{AppendixA} shows the results of testing primality for large numbers, specifically, Proth and Mersenne numbers. Only applicable algorithms amongst these algorithms are selected.

\subsection{Experimental Setup for Comparing Java with Python}
To make a fair comparison between Java and Python, regardless of the primality test types, the same pseudocode for each algorithm is implemented for both languages, and the same numbers to be tested are used. We try as much as possible to use the built-in functions in both languages to calculate the modulus arithmetic operations. The results are depicted in both Figure~\ref{fig1:javaPython1} and Figure~\ref{fig2:javaPython2}; pre-letter 'P' or 'J' before an algorithm name denotes the Python or Java implementation of that algorithm, respectively. Whereas post-letter 'P' or 'C' after a number denotes whether the number is prime or composite, respectively. The results show that Python outperforms Java in all of the cases. Nonetheless, Java is still preferable for such systems because of its ability to support special classes to deal with massive numbers (e.g. BigInteger class), moreover, the portability of Java which allows Java code to run on any operating system, is an apparent preference. Thus, for all the next sections we use Java as a programming language.

\begin{figure}[H]
\centering
\includegraphics[width=15cm]{Images/JavavsPython1.jpg}
\caption{Five Primality Tests Comparison Java and Python Performance. Pre “J” means Java implementation, and “P” means Python Implementation}
\label{fig1:javaPython1}
\end{figure}
\begin{figure}[H]
\centering
\includegraphics[width=15cm]{Images/JavavsPython2.jpg}
\caption{Three Primality Tests Comparison Java and Python Performance. Pre “J” means Java implementation, and “P” means Python Implementation}
\label{fig2:javaPython2}
\end{figure}
\subsection{Experimental Setup for Monto-Carlo Tests}
For the Monto-Carlo primality tests (Fermat, Miller-Rabin, Solovay-Strassen), six different numbers with different digit sizes are used as inputs for these tests. These numbers are listed in Table ~\ref{table2:MontoCarlo}.
\begin{table}[H]
\caption{Different Generic Numbers.}
\label{table2:MontoCarlo}
\centering

\begin{tabular}{|m{0.20\linewidth}|m{0.10\linewidth}|m{0.15\linewidth}|}
\toprule
\textbf{Value} &	\textbf{\#digits} &	\textbf{Type} \\
\midrule
11621 & 5 & Prime\\
\midrule
 11611 &	5 &	Composite	\\
 \midrule
 2860486327 &	10 &	Prime\\
 \midrule
 2860486317	& 10 &	Composite\\
 \midrule
 12764787846358441471 &	20 &	Prime\\
 \midrule
12764787846358441481 &	20 &	Composite\\
\bottomrule
\end{tabular}
\end{table}
Figure~\ref{fig3:MontoCarlo1} shows the results from the perspective view of the efficiency of the primality tests and the results show that Miller is the fastest. Fermat is always faster than Solovay but the latter is more accurate. Miller-Rabin test is always preferable from among all of these tests due to its speed and accuracy. An important observation should be noted from Figure~\ref{fig3:MontoCarlo1} is that these tests are considered as compositeness tests, thus the 20-digit composite number takes less time than the 5-digit  prime number.
\begin{figure}[H]
\centering
\includegraphics[width=13cm]{Images/MontoCarlo1.jpg}
\caption{Comparison of Monto-Carlo randomized primality tests}
\label{fig3:MontoCarlo1}
\end{figure}
\subsection{Experimental Setup for Las-Vegas Test}
Proth, Lucas, and Picklington tests are randomized Las-Vegas primality tests which means that they always give the right answer. Four different Proth numbers in the form $(K\ .\ 2^n + 1)$ with two different digit sizes are used as inputs for these tests. These numbers are listed in Table~\ref{table3:LasVegas}.
\begin{table}[H]
\caption{Different Proth numbers values.}
\label{table3:LasVegas}
\centering

\begin{tabular}{|m{0.24\linewidth}|m{0.10\linewidth}|m{0.15\linewidth}|}
\toprule
\textbf{Value} &	\textbf{\#digits} &	\textbf{Type} \\
\midrule
18433 $(9\ .\ 2^{11} + 1)$ &	5 &	Prime\\
\midrule
 45057 $(11\ .\  2^{12} +1)$ &	5	& Composite	\\
 \midrule
 2281701377 $(17\ .\  2^{27}+1)$ &	10	&Prime\\
 \midrule
 6710886401 $(25\ .\  2^{28} +1)$ &	10 &	Composite\\
\bottomrule
\end{tabular}
\end{table}
Figure~\ref{fig4:LasVegas} shows the results of comparing these tests. The results show that Proth test outperforms other tests. The reason behind that is due to the $n-1$ factorization needed in both Lucas and Picklington tests; indeed, this factorization may add a large computation overhead. Lucas and Pocklington could be preferable over Proth when the number to be determined is not in the Proth form since the Proth test’s certainty is approved by Proth numbers only, while Lucas and Picklington tests are able to check any generic number. From the figure it can also be observed that these tests can decide much more quickly the primality of a prime number than the compositeness of a composite number.
\begin{figure}[H]
\centering
\includegraphics[width=13cm]{Images/LasVegas1.jpg}
\caption{Comparison of Las-Vegas randomized primality tests (proth numbers as the inputs)}
\label{fig4:LasVegas}
\end{figure}
Figure~\ref{fig5:ProthvsMiller} shows the results of applying Miller-Rabin and Proth tests on composite Proth numbers. The results show that Miller is much faster approves the compositeness. Therefore, it is always suggested and advised when testing a number for primality, a compositeness test is applied first, and if the number passes that test with a probable prime decision then a Las-Vegas test could be applied for approving the primality.

\begin{figure}[H]
\centering
\includegraphics[width=13cm]{Images/ProthVsMiller.png}
\caption{Proth vs. Miller tests for composite numbers}
\label{fig5:ProthvsMiller}
\end{figure}

Although Figure~\ref{fig4:LasVegas} shows that Lucas outperforms Pocklington, this is not always the case. Pocklington is fast and smart when there is a $q$ prime factor of $n-1$ , such that  $q>\sqrt{(n-1)}$. For example, a number $n=18439$ has a prime factor $q=439 >  (\sqrt{(n-1)}=135)$ . Figure~\ref{fig6:LucasVsPock} shows that Pocklington accomplished the approving of primality of a 18439 much faster than Lucas (about 50\% faster).

\begin{figure}[H]
\centering
\includegraphics[width=13cm]{Images/LucasVsPock.png}
\caption{Lucas vs Pocklington when a prime factor $q>\sqrt{(n-1)}$ is available}
\label{fig6:LucasVsPock}
\end{figure}
\subsection{Experimental Setup for Deterministic Tests}
For a fair comparison between deterministic tests, distinction should be made between Pepin and Lucas-Lehmer. While  Pepin specilaized in the primality detection of Fermat numbers, Lucas Lehmer algorithm is designed to detect primality  for Mersenne numbers and hence, two different experiments are conducted to compare these types of primality tests. The Naïve and AKS are general deterministic tests for any format.

Fermat numbers which are listed in Table~\ref{table4:Deter1} are used as inputs for comparing between Pepin, Naïve, and AKS tests. The results that are depicted in Figure~\ref{fig7:det1} show that Pepin is the best for testing Fermat numbers. This result is only true for small and moderate numbers, whereas, for Massive integers, AKS should be the best choice. Furthermore, if Pepin test is used to test non-Fermat numbers, it gives a false negative in many cases. For example, Pepin declares 7 and 11 as composite numbers. Also, Naïve can also be fast if the input number is composite and has a small factor, then the test will exit very early. In general these tests can usually detect a composite number at an early stage.
\begin{table}[H]
\caption{Fermat numbers parameters}
\label{table4:Deter1}
\centering

\begin{tabular}{|m{0.30\linewidth}|m{0.10\linewidth}|m{0.15\linewidth}|}
\toprule
\textbf{Value} &	\textbf{\#digits} &	\textbf{Type} \\
\midrule
65537 $(2^{16} + 1)$ & 5 & Prime\\
\midrule
 32769 $(2^{15} + 1)$ &	5 &	Composite	\\
 \midrule
 4294967297 $(2^{32} + 1)$ &	10 &	Composite\\
 \midrule
 18446744073709551617 $(2^{64} + 1)$ 	& 20 &	Composite\\
\bottomrule
\end{tabular}
\end{table}

\begin{figure}[H]
\centering
\includegraphics[width=13cm]{Images/Deter1PepinNaiveAKS.jpg}
\caption{Deterministic primality tests comparison (Fermat numbers as inputs).}
\label{fig7:det1}
\end{figure}
Figure~\ref{fig8:det2} shows how the computations dramatically increased in the Naïve method when the number of digits of a prime input number are increased. On the other hand, an input composite number of 10-digit or 20-digit takes much less time than a 5-digit prime number. This is because the 10- and 20-digit composite numbers are divided by 3 and 7 respectively, which means that the test exits at a very early stage.
\begin{figure}[H]
\centering
\includegraphics[width=13cm]{Images/Det2Naive.jpg}
\caption{Naïve primality test behaviors (small to large numbers).}
\label{fig8:det2}
\end{figure}

Since Fermat is a Proth number with $k=1$, Figure~\ref{fig9:det3} shows the results of comparing Proth with Pepin  for testing Fermat numbers. The results show that Proth performs very well when the Fermat number is prime. However, since the largest discovered Fermat prime is 65537~\cite{OEISA0194342010}, we can figure out that Proth test is not prefarable for testing Fermat numbers unless a person wants to discover a new massive Fermat prime, if such prime is available!
\begin{figure}[H]
\centering
\includegraphics[width=13cm]{Images/det3PepinVsProth.jpg}
\caption{Pepin vs Proth for testing Fermat numbers.}
\label{fig9:det3}
\end{figure}

Mersenne numbers, that are listed in Table~\ref{table5:Mersenne}, are used as inputs for comparing Lucas-Lehmer, Naïve, and AKS. The results which are depicted in Figure~\ref{fig10:det4} show that Lucas-Lehmer outperforms the other two tests. Lucas-Lehmer is widely used in practice to test the primality of very large Mersenne numbers. In general, AKS is very slow in practice and as the number of digits increased, the computations dramatically increased. AKS even works worse than Naïve approach till a certain point (about $2^{64}$) then it beats Naïve.
\begin{table}[H]
\caption{Characteristics of 4 Mersenne numbers.}
\label{table5:Mersenne}
\centering

\begin{tabular}{|m{0.25\linewidth}|m{0.10\linewidth}|m{0.15\linewidth}|}
\toprule
\textbf{Value} &	\textbf{\#digits} &	\textbf{Type} \\
\midrule
8191 $(2^{13}-1)$ & 4 & Prime\\
\midrule
 2047 $(2^{11}-1)$ &	4 &	Composite	\\
 \midrule
 2147483647  $(2^{31}-1)$ &	10 &	Prime\\
 \midrule
 137438953471 $(2^{37}-1)$ 	& 12 &	Composite\\
\bottomrule
\end{tabular}
\end{table}
\begin{figure}[H]
\centering
\includegraphics[width=13cm]{Images/det4LucasLeh.png}
\caption{Comparison of 3 deterministic primality tests (Mersenne numbers as inputs).}
\label{fig10:det4}
\end{figure}
\subsection{Experimental Setup for Huerestic Test}
Since Baillie-PSW is a promised test for any number format, it should be compared to the most efficient former tests. For this scenario, Baillie test is compared with Miller for generic numbers, Proth for Proth numbers, Lucas-Lehmer for Mersenne numbers, and Pepin for Fermat numbers.

Figure~\ref{fig11:Baillie1} shows the results of comparing Baillie with Miller for random prime and composite numbers. The results show that Baillie outperforms Miller in all cases.
\begin{figure}[H]
\centering
\includegraphics[width=13cm]{Images/BaillieVsMiller.jpg}
\caption{Baillie vs the most efficient Monto-Carlo primality test (Miller-Rabin).}
\label{fig11:Baillie1}
\end{figure}

Figure~\ref{fig12:Baillie2} shows the results of comparing Baillie with Proth for Proth numbers listed in Table~\ref{table4:Deter1}. The results show that Baillie outperforms Proth in the case of determining compositeness of a composite number.
\begin{figure}[H]
\centering
\includegraphics[width=13cm]{Images/BaillieVsProth.jpg}
\caption{Baillie vs the most efficient Las-Vegas primality test (Proth).}
\label{fig12:Baillie2}
\end{figure}

Figure~\ref{fig13:Baillie3} the result of comparing Baillie with Pepin for three different Fermat numbers, whereas, Figure~\ref{fig14:Baillie4} shows the result of comparing Baillie with Lucas-Lehmer for four Mersenne numbers.
\begin{figure}[H]
\centering
\includegraphics[width=13cm]{Images/BaillieVsPepin.jpg}
\caption{Baillie vs the most efficient primality test for Fermat numbers (Pepin).}
\label{fig13:Baillie3}
\end{figure}
\begin{figure}[H]
\centering
\includegraphics[width=13cm]{Images/BaillieVsLucas.png}
\caption{Baillie vs the most efficient test for Mersenne numbers (Lucas-Lehmer).}
\label{fig14:Baillie4}
\end{figure}
\section{Conclusion and Future Work}
\label{section:Conclusion}
In this paper, a comprehensive survey of primality testing algorithms is presented including characteristics, features, limitations, and time complexity for each test. These primality tests are classified into four categories: Monto-Carlo randomized, Las-Vegas randomized, Deterministic, and Heuristic tests. Moreover, eleven of these algorithms are implemented by both Java and Python to compare their efficiency and the results show that Python always outperforms Java. By comparing the primality testing algorithms, regardless of the implementation language, the results show that no single primality test is adequate for all the cases and all of the numbers formats. For each case, an appropriate algorithm amongst these algorithms should be chosen.

For future work, we suggest implementing more primality algorithms, and using very large input numbers.

\bibliographystyle{unsrt}
\bibliography{references}

config: arxiv


TITLE: $f>0$ on real line ; $f(x+y)\le f(x)f(y) , \forall x,y \in \mathbb R$ ; $f([0,1])$ is bounded set ; does $\lim_{x \to \infty}(f(x))^{1/x}$ exist?
QUESTION [7 upvotes]: Let $f: \mathbb R \to (0,\infty)$ be a function such that $f(x+y)\le f(x)f(y) , \forall x,y \in \mathbb R$ and $f$ is bounded on $[0,1]$ ; then does the limit $\lim_{x \to \infty}(f(x))^{1/x}$ exists ?
What I have found is $f(x)\le f(x/n)^n , \forall x \in \mathbb R , \forall n \in \mathbb N$ ; so say if $f(x) < M , \forall x \in [0,1]$ then we
get $f(x)\le f\Big(\dfrac x{[x]+1}\Big)^{[x]+1}\le M^{[x]+1} \le M^{2x} , \forall x>1$ ; so that $(f(x))^{1/x}$ remains bounded for large $x$ .
Please help . Thanks in advance .

REPLY [0 votes]: At first,
$$\ln\lim_{x\to+\infty}{f(x)^{1/x}} = \lim_{x\to+\infty}\frac{\ln{f(x)}}{x},\quad x>0, f(x)>0.$$
Let
$$g(x)=\frac{\ln{f(x)}}{x},$$then
$$(x+\theta)g(x+\theta)<xg(x)+\theta g(\theta),\quad \theta\in[0,1], x\in(0,\infty),$$
$$\begin{cases}
(x+\theta)(g(x+\theta)-g(x))\leq\theta(g(\theta)-g(x)),\quad \theta\in[0,1], x\in(0,\infty),\\
(x+\theta)(g(x+\theta)-g(\theta))\leq x(g(x)-g(\theta)),\quad \theta\in(0,1], x\in(0,\infty),
\end{cases}
$$
$$
\begin{cases}
g(x+\theta)\leq g(x),\text{ when }g(\theta)\leq g(x),\\
g(x+\theta)\leq g(\theta), \text{ when }g(x)\leq g(\theta).
\end{cases}
$$
So
$$g(x+\theta)\leq\max(g(x),G),\quad x\in(0,\infty),\quad G=\max_{\theta\in[0,1]} g(\theta).$$
Thus, $g(x)$ is bounded above for $x\in(0,\infty).$
When $g(x)\to -\infty,$ then $f(x)^{1/x}\to0,$ and we can account that $g(x)$ is lower bounded for $x\in(0,\infty).$
So, required limit exists if g(x) is monotonic.


TITLE: Find a recurrence relation problem...
QUESTION [1 upvotes]: Well... I am stuck with the recurrence problem...

Find a recurrence relation for the number Queen walks from the lower-left corner of an arbitrary-size chess board to the square $(n,n)$. (A Queen can move any number of squares horizantally, vertically, or diagonally. Assume that the Queen always move to the right,up, or up-right.) Find the corresponding generating function.

I think I have to split the cases with the movements(right, up, or up-right.)
It becomes tricky with that a Queen can move any number of squares...
Can I get some clue to start?

REPLY [0 votes]: See, that from position $(a,b)$ the queen can move right in $n-b$ ways, up in $n-a$ ways and 'right-up' in $\min(n-a, n-b)$ ways.
Therefore the recursive function can be written as
public int count=0;
public int n;
public void main()
{
  n=8; //here you can set your n
  count =0;
  movements(1,1);
  Console.WriteLine(count.ToString());
}
public void movements(int a, int b)
{
  if(a==n && b==n)
  {
    count++;
    return;
  }
  for(int i=a+1; i<=n; ++i)
    movements(i, b);
  for(int i=b+1; i<=n; ++i)
    movements(a, i);
  for(int i=1 ; i<=Math.Min(n-a, n-b); ++i)
    movements(a+i, b+i);
}

or, in mathematical way:
$f(a,b,n) =  \begin{cases}1 &, (a,b)=(n,n);\\ \sum\limits_{i=a+1}^nf(i,b,n)+\sum\limits_{i=b+1}^n f(a,i,n) + \sum\limits_{i=1}^{\min(n-a,n-b)}f(a+i,b+i,n) &, (a,b)\neq (n,n);  \end{cases}$


TITLE: Why is $\sum \frac{\sqrt{n^3+n}-\sqrt{n^3}}{\sqrt{n(n+1)}-1}$ convergent?
QUESTION [1 upvotes]: We have the following series:
$$\sum_{n=1}^{\infty} \frac{\sqrt{n^3+n}-\sqrt{n^3}}{\sqrt{n(n+1)}-1}$$
According to WolframAlpha it is convergent but I don't see why. Intuitively, the expression under the sum must be assymptotically similair to something like $\frac{1}{n^\alpha}$, and clearly $\sqrt{n^2}$ dominates the denominator. In the numerator the expression looks similair either to $\sqrt{n^3}$ but $\frac{\sqrt{n^3}}{\sqrt{n^2}}={\sqrt{n}}$ which gives as assymptotic similarity to sequence, the sum of which is divergent. On the other hand, if $\sqrt{n}$ dominates the numerator  we get $\frac{\sqrt{n}}{\sqrt{n^2}}=\frac{1}{\sqrt{n}}$ so again assymptotic similarity to sequence, which gives divergent sum.
So why is this sum convergent anyway?

REPLY [1 votes]: We have
$$\frac{\sqrt{n^3+n}-\sqrt{n^3}}{\sqrt{n(n+1)}-1}\sim_\infty\frac{n^{3/2}((1+n^{-2})^{1/2}-1)}{n}\sim_\infty\frac1{2n^{3/2}}$$
and the Riemann series
$$\sum \frac1{n^{3/2}}$$
is convergent so the given series is convergent by asymptotic comparison.