On Generating Prime Numbers Efficiently

On Generating Prime Numbers Efficiently

On Generating Prime Numbers Efficiently Juhani Sipilä School of Science Thesis submitted for examination for the degree of Master of Science in Technology. Espoo 2.6.2018 Supervisor Assistant Prof. Pauliina Ilmonen Advisor Dr Vesa Kaarnioja Copyright © 2018 Juhani Sipilä Aalto University, P.O. BOX 11000, 00076 AALTO www.aalto.fi Abstract of the master’s thesis Author Juhani Sipilä Title On Generating Prime Numbers Efficiently Degree programme Mathematics and Operations Research Major Systems and Operations Research Code of major SCI3055 Supervisor Assistant Prof. Pauliina Ilmonen Advisor Dr Vesa Kaarnioja Date 2.6.2018 Number of pages 56+4 Language English Abstract The prime numbers can be considered as the building blocks of natural numbers, having innumerable applications in number theory and cryptography. There exist multiple different sieving algorithms for the generation of prime numbers. In this thesis, an elementary modular result is utilized to construct an analytically useful generator function and its inverse function. The functions are used to generate a (log)log-linear time complexity prime sieving algorithm which is further optimized to be of linear time complexity. The constructed algorithms and their operation are studied and the linear implementations in JS, Python and C++ are compared to other prime sieves. Keywords Prime number, prime sieve, algorithm, Myriad sieve Aalto-yliopisto, PL 11000, 00076 AALTO www.aalto.fi Diplomityön tiivistelmä Tekijä Juhani Sipilä Työn nimi On Generating Prime Numbers Efficiently Koulutusohjelma Mathematics and Operations Research Pääaine Systems and Operations Research Pääaineen koodi SCI3055 Työn valvoja Apulaisprof. Pauliina Ilmonen Työn ohjaaja TkT Vesa Kaarnioja Päivämäärä 2.6.2018 Sivumäärä 56+4 Kieli Englanti Tiivistelmä Alkulukuja voidaan pitää luonnollisten lukujen rakennuspalikoina joilla on lukematto- mia sovelluksia lukuteoriassa ja kryptografiassa. Alkulukujen luomiseen on olemassa useita erilaisia seulonta-algoritmeja. Tässä opinnäytetyössä käytetään modulaarista perustulosta analyyttisesti hyö- dyllisten kehitysfunktion ja sen käänteisfunktion luomiseen. Funktioiden avulla luodaan aikakompleksisuudeltaan (log)log-lineaarinen alkulukuseula, joka optimoi- daan lineaariseksi. Rakennettuja algoritmeja ja niiden toimintaa tarkastellaan ja lineaarista implementaatiota JS, Python ja C++ ohjelmointikielillä verrataan toisiin alkulukuseuloihin. Avainsanat Alkuluku, alkulukuseula, algoritmi, Myriad seula “The problem of distinguishing prime numbers from composite numbers and of resolving the latter into their prime factors is known to be one of the most important and useful in arithmetic. It has engaged the industry and wisdom of ancient and modern geometers to such an extent that it would be superfluous to discuss the prob- lem at length... Further, the dignity of the science itself seems to require that every possible means be explored for the solution of a problem so elegant and so celebrated.” C.F. Gauss, Disquisitiones Arithmeticae, article 329 (1801) 6 Contents Abstract3 Abstract (in Finnish)4 Contents6 Table of notations8 1 Introduction9 1.1 Divisibility.................................... 10 1.2 Prime numbers and factoring......................... 10 1.3 Modular results related to this work.................... 11 1.4 Important results and history relating to primes............. 12 2 Background on sieves 14 2.1 Sieve of Eratosthenes............................. 14 2.2 Linear sieve................................... 16 2.3 Sub-linear sieve with wheels......................... 18 2.4 Segmented sieve................................ 19 2.5 Modern sieves.................................. 20 2.6 Primality testing................................ 21 3 The Myriad sieving algorithm 23 3.1 The geometrical pattern............................ 23 3.2 The generator function............................ 26 3.3 Composite generating indices........................ 29 3.4 The initial sieving algorithm......................... 31 3.5 Linear sieving algorithm........................... 34 3.6 Remarks on the algorithms.......................... 36 3.7 Other potential uses of the framework................... 36 4 Analysis of the linear algorithm 38 4.1 Analysis of the search space of the linear algorithm........... 38 4.2 Implementations................................ 46 4.2.1 JavaScript............................... 46 4.2.2 Python................................. 47 4.2.3 C++................................... 47 4.3 Results...................................... 48 4.3.1 JavaScript............................... 49 4.3.2 Python................................. 49 4.3.3 C++................................... 49 4.4 Potential further optimizations....................... 53 5 Conclusions 54 References 55 A C++ implementation of the linear Myriad sieve 57 B JavaScript implementation of the linear Myriad sieve 59 Table of notations N : The set of natural numbers, {1,2,3,...}. P : The set of prime numbers, {2,3,5,7,11,...}. G : The set of numbers of the form 6m ± 1 (m ∈ N). i.e. all natural numbers greater than 1 which do not have a factor of 2 or 3. G∗ : Composite numbers in G. The set of numbers which are expressible as a product of two (or more) elements from G. pn : The n:th prime number. gcd(n, m) : Greatest common divisor: αi βi min{αi,βi} n = ∏ pi , m = ∏ pi , gcd(n, m) = ∏ pi (αi, βi ≥ 0). pi∈P pi∈P pi∈P φ(n) : Euler’s totient function. The number of integers m such that 1 ≤ m ≤ n for which gcd(n, m) = 1. a ≡ n mod m : Modular congruence: m divides a with a remainder of n. G(n) : The generator function, producing all numbers of the form 6m ± 1 (m ∈ N) in ascending order. G−1(g) : The inverse of the generator function, producing the index of g ∈ G. 1 Introduction This thesis starts by introducing and defining the elementary definitions and necessary concepts for the presented prime generating algorithm. We start by discussing the basic concepts and terminology related to the prime numbers. We then explore and discuss certain important known results relating to the prime numbers. The applications of the prime numbers are also discussed briefly. In chapter2 we take a look at important results of prime generating algorithms. We introduce the oldest and probably the best known prime generating algorithm, the sieve of Eratosthenes, and analyze its behaviour. The sieve of Eratosthenes can be seen as the basis for improved and more advanced variations which are also discussed. The name sieve comes from the fact that these types of algorithms are sifting composite numbers (non prime numbers) out of an interval, in such a way that in the end only prime numbers remain in the sieve. The analogy comes from the process of sifting gold from less precious ores. We also introduce various linear time complexity variations and introduce the concept of a factor wheel, which decreases the asymptotic time complexity to be sublinear. Probabilistic sieves are discussed briefly along with other probabilistic primality testing methods. These primality tests could also be used to generate prime numbers but the sieving methods are usually more appropriate for intervals as opposed to testing whether a given number is prime or not. In chapter3 we introduce the geometrical pattern observed in the squares of prime numbers, which was the original inspiration for the development of our prime sieve. This geometrical pattern gives rise to the ideas of a prime generator function G(n), its range G and an indexing function I(x, y) which allows one to determine all composite numbers in G. These concepts are then used to construct a (log)log-linear prime sieve, similar to the sieve of Eratosthenes. This sieve is then optimized to be of linear time complexity. The pseudocode algorithms for these sieves are presented. Chapter4 is devoted to visualizing and analyzing the operation of the linear sieve. We also implement the linear sieve in JavaScript, Python and C++11 and measure the execution times of the implementations comparing them to other sieves. Lastly we discuss some potential further optimizations and improvements ending with conclusions. The implemented JavaScript and C++11 sieves can be found in the appendices or in GitHub (https://github.com/JuhaniSipila/MyriadPrimes). 10 1.1 Divisibility The natural numbers form the primary subject matter of arithmetic. The process of dividing a natural number N into M equally sized parts has many simple but fascinating properties. For some integers N there is no way to divide it by M (without remainder) into smaller equally sized parts. For other N there are multiple distinct M such that the division is possible. Definition 1.1 (Divisibility [2]). An integer a is said to be divisible by another integer b if there is a third integer c such that a = b ⋅ c. The integers b and c are called divisors or factors of a. For example the number 12 can be expressed as 12 = 1 ⋅ 12 = 2 ⋅ 6 = 3 ⋅ 4 and thus 12 is divisible by 1,2,3,4,6 and 12. The number 13 on the other hand doesn’t divide into smaller equal integersized portions and is only trivially divisible by 1 and 13 itself. Divisibility is closely related to primality and next we shall see why numbers which are only divisible by 1 and themselves are considered so fundamental in mathematics. 1.2 Prime numbers and factoring Definition 1.2 (Prime numbers [2]). A prime number (or simply a prime) is a natural number greater than 1, which has no positive divisors other than 1 and itself. A natural number greater than 1 that is not a prime

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    60 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us