APM_2F005 - Algorithms For Discrete Mathematics Bachelor 2
Lecturer: Lucas Gerin

Experimental Maths 1: Primes and Euclid

Table of contents

1. Primes and factorization

Prime numbers and divisibility

We aim to investigate the distribution of primes among integers. Namely, how many prime numbers are there (approximately) between $1$ and $n$?

Write a boolean function IsPrime(n) which returns True if and only n is prime.
(In python $a\ (\mathrm{mod}\ p)$ is obtained with `a%p`.)

Now we are ready for experiment. For $n\geq 2$, let $\pi(n)$ denote the number of primes less or equal to $n$. For example, $\pi(11)=5$ since $2,3,5,7,11$ are prime.

1. Write a script which takes as input $n$ and returns the list $[\pi(2),\pi(3),\dots,\pi(n)]$. 2. Plot the function $n \mapsto \pi(n)$ (try $n=100,1000,10000$).
Modify your previous plot to illustrate the Prime Number Theorem: $$ \pi(n)\sim \frac{n}{\log(n)}. $$
According to the above plot, we have something like $$ P(n) \sim C\times \frac{n}{\log(n)} $$ where $C\approx 1.13$ is indeed close to $1$.

Factorization

Write a function Factorize(n) which returns the factorization of $n$ into primes. For example your function should return:

Factorize(2158884)
[2, 2, 3, 3, 7, 13, 659]

Hint: Think recursive!