Fool's algorithm for finding prime numbers

Revision en1, by alexvim, 2024-04-01 01:47:00

\documentclass[12pt]{article} \usepackage[utf8]{inputenc} \usepackage[english]{babel} \usepackage[margin=1in]{geometry}

\usepackage{minted}

\usepackage{blindtext} \usepackage{amssymb} \usepackage{hyperref} \hypersetup{ colorlinks=true, linkcolor=blue, filecolor=magenta,
urlcolor=cyan, }

%% COLOR DEFINITIONS

\usepackage{xcolor,sectsty}

\definecolor{astral}{RGB}{46,116,181} \subsectionfont{\color{astral}} \sectionfont{\color{astral}}

\urlstyle{same}

\usepackage{amsthm} \usepackage{amsmath} %\newtheorem{theorem}{Theorem} \newtheorem{theorem}{Statement}[section] \newtheorem{corollary}{Corollary}[theorem] \newtheorem{lemma}[theorem]{Lemma}
\theoremstyle{remark} \newtheorem*{remark}{Remark}

\theoremstyle{definition} \newtheorem{definition}{Definition}[section]

\title{\color{astral}Analysis of the algorithm for finding prime numbers based on long arithmetic} \author{Alexey Zlobin (alexvim)} \date{1 April 2024}

\begin{document}

\maketitle \begin{abstract} We will observe the trivial and probably the shortest (but not fast) algorithm for finding all prime numbers $$$p \in \mathbb{N}: p \leq n$$$ based on Wilson's theorem and long arithmetic. \end{abstract}

\section{Wilson's theorem} Algorithm based on well-known in number theory Wilson's theorem. Often implication is being proved only from left to right but it is important to prove equivalence.

\begin{theorem} $$$p \in \mathbb{N}: p \ge 2$$$ is prime if and only if [ (p-1)! \equiv -1 \pmod{p}. ] \end{theorem}

\begin{proof} \textbf{\item $$$\rightarrow$$$} \quad p is prime so $\forall a \in \mathbb{Z}/p\mathbb{Z} \quad \exists a^{-1} \in \mathbb{Z}/p\mathbb{Z}: a\cdot{a^{-1}}=1$. Therefore in product $$$1 \cdot 2 \cdot \dotso \cdot (p-1)$$$ all elements are divided in pairs $$$(a, b)$$$ such that $$$ab=1$$$. \textbf{\item $$$\leftarrow$$$} \quad Let's assume that p is composite. If p is not full square: $$$p \neq a^2 \quad \forall a \in \mathbb{Z}$$$ then $$$\exists a,b \in \mathbb{N}: a,b < p$$$ and $$$ab=p$$$ so $$$p=ab$$$ divides $$$(p-1)!$$$. Else if $$$p=a^2: a>1$$$ then a divides $$$gcd(p, (p-1)!)$$$ so a divides $$$(p-1)!$$$ mod $$$p$$$ and as a result: $$$(p-1)! \not\equiv -1 \pmod{p}$$$ — contradiction. \end{proof}

\section{Algorithm} From Wilson's theorem can be derived very simple algorithm for finding prime numbers. \begin{listing} \begin{minted}[linenos]{Python} a = 1 for i in range(2, n): if a%i == i-1: pass #prime a *= i \end{minted} \end{listing}

\section{Long arithmetic} It is easy to notice that algorithm needs long arithmetic because factorial grows very fast. The one of fastest implementations of long arithmetic uses FFT (Fast Fourier Transform) for numbers (or polynomials) multiplication. FFT multiplies $$$a\cdot{b}$$$ in $$$O(n\log{n})$$$ where n is max bit-length of a and b. We will use this fact in analysis. P.S. FFT with heuristics is used in Python too.

\section{Asymptotic} Notice that at iteration $$$k$$$ $$$a$$$ length is $$$O(\log{k!})$$$ so entire complexity is $$$O(\sum_{k=2}^n{\log{k!}\cdot{\log{\log{k!}}})}$$$

\begin{theorem} $$$\sum_{k=2}^n{\log{k!}\cdot{\log{\log}k!}} = O(n^2\log^2{n})$$$ \end{theorem}

\begin{proof} Using Stirling's formula \cite{ST} for gamma function (factorial) logarithm [\log{k!} = k\log{k} — k + O(\log{k}) = k\log{k} + O(k)] we get: [\sum_{k=2}^n{\log{k!}\cdot{\log{\log{k!}}}} = \sum_{k=2}^n{(k\log{k} + O(k))\log{(k\log{k} + O(k))}}] Using fact $$$\log{(f + o(f))} = \log{f} + o(\log{f})$$$ (can be clearly checked according to the L'Hospital rule \cite{LH}) we get that the sum \textbf{asymptotically} equals to: [\sum_{k=2}^n{k\log^2{k}}] There is Euler-Maclaurin's summation formula \cite{EM} $$$\sum_{k=a}^b{f(k)} = \int_a^b{f(x)dx} + O(f)$$$ for class functions $$$f$$$ such that $$$f'(x) = O(f)$$$ or $$$f'(x) = o(f)$$$. So we can approximate discrete sum with integral: [\sum_{k=2}^n{k\log^2{k}} = O(\int_2^n{x\log^2{x}dx})] After applying integration by parts two times we get that value of this integral is $$$O(n^2\log^2{n})$$$, Q.E.D. \end{proof}

\section{Conclusion} This algorithm is rather absurd but it shows some simple techniques in work with long arithmetic.

\bibliographystyle{alpha} \bibliography{biblio} \end{document}

Tags math, prime numbers, asymptotics, humour

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
ru3 Russian alexvim 2024-04-01 13:08:34 108
en4 English alexvim 2024-04-01 13:07:44 27
ru2 Russian alexvim 2024-04-01 12:38:27 0 (опубликовано)
en3 English alexvim 2024-04-01 12:37:46 0 (published)
ru1 Russian alexvim 2024-04-01 12:37:22 3504 Первая редакция перевода на Русский (сохранено в черновиках)
en2 English alexvim 2024-04-01 11:52:36 1898
en1 English alexvim 2024-04-01 01:47:00 4427 Initial revision (saved to drafts)