arithmetic 1: prime numbers and factorization...

12
Bachelor of Ecole Polytechnique Algorithms for Discrete Mathematics, year 2, semester 1 Arithmetic 1: Prime numbers and factorization (with Solutions) P. Tchebychev (Russia, 1821-1894). Among many things, he was the �rst mathematician to obtain the asymptotics for the number of prime numbers less than (see the �rst Exercise below). n Table of contents Primes and factorization Prime numbers and divisibility Factorization 1st digit and powers of two A mysterious function (Bonus) Pseudo-random numbers # execute this part to modify the css style from IPython.core.display import HTML def css_styling(): styles = open("./style/custom2.css").read() return HTML(styles) css_styling()

Upload: others

Post on 19-Jun-2020

15 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Arithmetic 1: Prime numbers and factorization …gerin.perso.math.cnrs.fr/Enseignements/Arithmetic1...Arithmetic 1: Prime numbers and factorization (with Solutions) P. Tchebychev (Russia,

Bachelor of Ecole PolytechniqueAlgorithms for Discrete Mathematics, year 2, semester 1

Arithmetic 1: Prime numbers andfactorization (with Solutions)

P. Tchebychev (Russia, 1821-1894). Among many things, he was the �rst mathematician toobtain the asymptotics for the number of prime numbers less than (see the �rst Exercise

below).n

Table of contentsPrimes and factorization

Prime numbers and divisibility

Factorization

1st digit and powers of two

A mysterious function

(Bonus) Pseudo-random numbers

# execute this part to modify the css stylefrom IPython.core.display import HTMLdef css_styling():

styles = open("./style/custom2.css").read()return HTML(styles)

css_styling()

Page 2: Arithmetic 1: Prime numbers and factorization …gerin.perso.math.cnrs.fr/Enseignements/Arithmetic1...Arithmetic 1: Prime numbers and factorization (with Solutions) P. Tchebychev (Russia,

Prime numbers and divisibility

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

Do it yourself. Write a boolean function IsPrime(n) which returns True if and only

is prime.

(In python is obtained with a%p .)

n

a (mod p)

Now we are ready for experiment. For , let denote the number of primes lessthan . For example, since are prime.

n ≥ 2 P(n)n P(11) = 5 2, 3, 5, 7, 11

Do it yourself.

Write a script which takes as input and returns the list .

Plot the function (try ).

n [P(2), P(3), … , P(n)]n ↦ P(n) n = 100, 1000, 10000

TrueFalse

## loading python libraries

# necessary to display plots inline:%matplotlib inline

# load the librariesimport matplotlib.pyplot as plt # 2D plotting libraryimport numpy as np # package for scientific computing from math import * # package for mathematics (pi, arctan, sqrt, factorial ...)

def IsPrime(n):# input: integer n# output: True or False depending on whether n is prime or notif n==1:

return Falseif n==2:

return Trueelif n%2==0:

return Falsefactor=3while factor**2 < n+1:

if n%factor == 0:return False

factor=factor+2return True

# Testprint(IsPrime(2))print(IsPrime(2019))

Page 3: Arithmetic 1: Prime numbers and factorization …gerin.perso.math.cnrs.fr/Enseignements/Arithmetic1...Arithmetic 1: Prime numbers and factorization (with Solutions) P. Tchebychev (Russia,

Do it yourself. Modify your previous plot to guess (by trials and errors) what is theasymptotic behaviour of when : �nd a sequence such that .

In order to improve your guess you can plot in some interval (instead of

).

P(n) n → +∞ an P(n) ∼ anPnan

(T/2, T)(0, T)

CountingPrimes=[0]T=2000

for n in range(2,T):if IsPrime(n):

CountingPrimes.append(CountingPrimes[-1]+1)else:

CountingPrimes.append(CountingPrimes[-1])plt.plot(range(1,T),CountingPrimes,label='pi')

#plt.plot(range(1,T),X/np.log(X+1))plt.xlabel('Number $n$'),plt.ylabel('Primes less than $n$')plt.title('Counting Primes')plt.show()

Page 4: Arithmetic 1: Prime numbers and factorization …gerin.perso.math.cnrs.fr/Enseignements/Arithmetic1...Arithmetic 1: Prime numbers and factorization (with Solutions) P. Tchebychev (Russia,

Answers. According to the above plot, we might guess something like

where is some constant. In fact the Prime Number Theorem(https://en.wikipedia.org/wiki/Prime_number_theorem) states that this is the case with

.

P(n) ∼ C × n

log(n)C ≈ 1.13

C = 1

Factorization

Do it yourself. 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!

1.15153672262

# By trial and errors we finally guess that pi(n) is close to n/log(n)

X=range(1,T)plt.plot(range(1,T),CountingPrimes*np.log(X)/X)plt.xlabel('Number $n$'),plt.ylabel('$P(n)/(n\log(n))$')plt.title('Our guess : n/log(n)')plt.show()R=CountingPrimes[-1]*np.log(T)/(T+0.0)print(R)

Page 5: Arithmetic 1: Prime numbers and factorization …gerin.perso.math.cnrs.fr/Enseignements/Arithmetic1...Arithmetic 1: Prime numbers and factorization (with Solutions) P. Tchebychev (Russia,

For we introduce

For example, .

n ≥ 2F(n) = Number of prime factors of n, counted with multiplicity.F(2158884) = 7

Do it yourself. Plot the function (try ).n ↦ F(n) n = 100, 1000, 5000

Do it yourself. (Theory)

Plot on the previous picture and (logarithm in basis two).1. Prove the following:

(lower bound) There are in�nitely many 's for which .

(upper bound) for every .

2. n ↦ F(n) n ↦ (n)log2

n F(n) = (n)log2

F(n) ≤ (n)log2 n

[2, 2, 3, 3, 7, 13, 659]

def Factorize(n):# input: integer n# output: list of factors of nfor factor in range(2,n): # Tests division by 2,3,...,n

if n%factor == 0:return [factor]+Factorize(n//factor)

return [n]

print(Factorize(2158884))

n_min=2n_max=200F=[len(Factorize(k)) for k in range(n_min,n_max)]X=range(n_min,n_max)plt.plot(X,F,'.',label='F(n)')plt.plot(X,np.log(X)/np.log(2),label='log_2(n)')plt.xlabel('Number $n$')plt.title('Number of prime factors')plt.legend()plt.show()

Page 6: Arithmetic 1: Prime numbers and factorization …gerin.perso.math.cnrs.fr/Enseignements/Arithmetic1...Arithmetic 1: Prime numbers and factorization (with Solutions) P. Tchebychev (Russia,

Answers.See the previous picture. One sees that is always below but they seemto coincide at

1.

When is a power of two,

Thus, for in�nitely many integers (the powers of two), .

For any integer , let us write

Factorize(n) .

Every factor in the above decomposition is . Therefore

i.e. for every .

2.

F(n) (n)log2n = 2, 4, 8, 16, 32, . . .

n = 2k

F( ) = k = (n)2k log2

F(n) ≥ (n)log2

n

= [ , , … ]a1 a2 aF(n)

ai ≥ 2n = × × ⋯ × ≥ ,a1 a2 aF(n) 2F(n)

F(n) ≤ (n)log2 n

1st digit and powers of two

Do it yourself. Write a function FirstDigit(n) which returns the leftmost digit of :

FirstDigit(238)

2

Hint: Think recursive!

n

5

def FirstDigit(n):# input: integer n# output: First digit (in {1,...,9})if n<10:

return nelse:

return FirstDigit(n//10)

# testFirstDigit(516)

Page 7: Arithmetic 1: Prime numbers and factorization …gerin.perso.math.cnrs.fr/Enseignements/Arithmetic1...Arithmetic 1: Prime numbers and factorization (with Solutions) P. Tchebychev (Russia,

Do it yourself.Fix large. Plot the histogram of the frequencies of among the leading

digits of .

1.

Are they equally distributed in ? What is the most frequent leadingdigit? What is the shape of this histogram? You can have a look at the Wikipedia'spage of the Benford distribution and compare with your plot.(A rigorous proof is beyond the level of Bachelor .)

2.

n {1, 2, … , 9}2, , , , , … ,22 23 24 25 2n

{1, 2, … , 9}

2

Answers. It can be proved with ergodic theory that the limiting frequencies (when) are governed by the Benford distribution :

(See this link (https://www.johndcook.com/blog/2017/06/20/leading-digits-of-powers-of-2/) for a nice account on this problem.)

n → +∞ B

Frequency of i B(i) := ((i + 1)/i).→n→+∞ log10

A mysterious function

Do it yourself. What does the following function return? Can you prove it?

n=100

FirstDigitPowersOfTwo=[FirstDigit(2**i) for i in range(n)]Benford=[np.log((k+1.0)/k)/np.log(10) for k in range(1,10)]

plt.plot(np.arange(1.5,10.5),Benford,'ro',label='Benford distribution')plt.hist(FirstDigitPowersOfTwo, bins= np.arange(1,11), normed=True, facecolor='g', alphaplt.legend()plt.show()

Page 8: Arithmetic 1: Prime numbers and factorization …gerin.perso.math.cnrs.fr/Enseignements/Arithmetic1...Arithmetic 1: Prime numbers and factorization (with Solutions) P. Tchebychev (Russia,

Answers. By testing a few cases one may conjecture that Mystery(n) returns as a list

of the decomposition of in basis two. To prove so let us write

where are the decomposition in basis two. We have that

where stands for the euclidian division, as in python. Finally the decomposition of is the concatenation of

The decomposition of

.

nn = + + ⋯ + × 2 +ak2k ak−12k−1 a1 a0

∈ {0, 1}ai

n = 2 × + , + + ⋯ + × 1ak2k−1 ak−12k−2 a1

=n//2

a0

a//2 n

n//2= n mod 2a0

Change of basis

Do it yourself. Write a function ChangeBasis(a,b,n_InBasis_a) which takes as

inputs:

Two basis (integers )

A list which gives the decomposition of some integer in basis

and which returns the decomposition of in basis .

For example

ChangeBasis(5,2,[4,1])

[1, 0, 1, 0, 1]

(Since in basis is i.e. .)

a, b ≥ 2n a

n b

[4, 1] 5 21 [1, 0, 1, 0, 1]

[1, 1, 1, 1, 0, 1, 1]

def Mystery(MysteriousVariable):# input: ???# output: ???if MysteriousVariable==0:

return []else:

return Mystery(MysteriousVariable//2) + [MysteriousVariable%2]

Page 9: Arithmetic 1: Prime numbers and factorization …gerin.perso.math.cnrs.fr/Enseignements/Arithmetic1...Arithmetic 1: Prime numbers and factorization (with Solutions) P. Tchebychev (Russia,

(Bonus) Pseudo-random integersFor simulation it is important to generate pseudo-random numbers. The goal of thefollowing exercise is to analyze one of the simplest random number generator: the linearcongruential generator (LCG).

This works as follows:

Fix integers .

Start your generator with .

For every , put

We expect that if are well chosen, then the sequence looks like a sequenceof independent and uniform random variables in .

m, a, b∈ {0, 1, … ,m − 1}x0

n ≥ 1= a + b (mod m).xn xn−1

m, a, c (xn)n≥0{0, 1, … ,m}

Remark. Usually the 's are divided by to get uniform random variables in .xn m (0, 1)

Do it yourself.

Write a script which returns the list . Take , ,.

1.

Draw a histogram which illustrates uniformity of the 's (take at least ).2.

[ , , … , ]x0 x1 xN m = − 1231 a = 75

b = 0xn N = 50000

[1, 0, 1, 0, 1]

def BasisToTen(n_InBasis_b,b):# input: b: basis# n_InBasis_b: decomposition of some integer n in basis b# output: integer nif n_InBasis_b == []:

return 0else:

return n_InBasis_b[-1] + b*BasisToTen(n_InBasis_b[0:-1],b)

def TenToBasis(n,b):# input: integer n, basis b# output: n in basis bif n==0:

return []else:

return TenToBasis(n//b,b) + [n%b]

def ChangeBasis(a,b,n_InBasis_a):# inputs: basis a,b, integer n in basis a (as a list)# output: n in basis b (as a list)n= BasisToTen(n_InBasis_a,a)return TenToBasis(n,b)

ChangeBasis(5,2,[4,1])

Page 10: Arithmetic 1: Prime numbers and factorization …gerin.perso.math.cnrs.fr/Enseignements/Arithmetic1...Arithmetic 1: Prime numbers and factorization (with Solutions) P. Tchebychev (Russia,

Do it yourself.(Theory)

Prove that for every the sequence is periodic. Can you give an upperbound for the period?

1.

Can you give very bad choices for ?2.

m, a, b ( )xn

m, a, b

Answers.As takes its values in a �nite set of size , there exists (at least) two indexes

such that . As the sequence is de�ned by a recurrence relation then

, , and so on.

This proves that has a period at most .

1.

Obviously:

very small (the period would be short).

if have a common factor then some numbers will never be chosen for .

2.

( )xn mi, j ≤ m =xi xj

=xi+1 xj+1 =xi+2 xj+2( )xn j − i ≤ m

m

a, b,m xn

N=50000m=2**31-1a=7**5b=0x_0=1

Sequence_x=[x_0]CurrentValue=x_0for n in range(1,N+1):

CurrentValue=(a*CurrentValue+b)%mSequence_x.append(CurrentValue)

plt.hist(Sequence_x, 50, normed=True, facecolor='g', alpha=0.2)plt.show()

Page 11: Arithmetic 1: Prime numbers and factorization …gerin.perso.math.cnrs.fr/Enseignements/Arithmetic1...Arithmetic 1: Prime numbers and factorization (with Solutions) P. Tchebychev (Russia,

It can be proved that

.

( ,  are independent uniform in {0, 1, … ,m − 1})X2i X2i+1 ⇔

(( , ) is uniform in {0, 1, … ,m − 1 )X2i X2i+1 }2

Do it yourself.Imagine a plot which illustrates the uniformity of pairs generated by theprevious linear congruential generator and code it. A good idea is to

plt.matshow(...)

1.

Try with , , then with . Doyou observe that the latter is a bad choice?

2.

( , )x2i x2i+1

m = − 1231 a = 75 b = 0 m = 20000, a = 177, b = 0

Answers.A possible graphical output is the following:

Fix an integer , and divide the square into identical sub-squares. Wede�ne a matrix of size which counts the number of pairs

in every sub-square:

In order to understand the code below, observe that

1.

We then plot this matrix (below we used plt.matshow(...) .) If are

uniformly distributed then the plot should be homogeneous.

The following plot does not show any particular pattern for , ,. On the opposite, a regular patterns clearly appears for

.

1.

P [0,m]2 P2

M (P + 1) × (P + 1)( , )x2i x2i+1

= card{i such that (k − 1) ≤ < k , (ℓ − 1) ≤ < ℓ }.Mk,ℓmPx2i

mP

mPx2i+1

mP

(k − 1) ≤ < k ⇔ k − 1 = Floor(P /m).mPx2i

mP

x2i

( , )x2i x2i+1

m = − 1231 a = 75

b = 0m = 20000, a = 177, b = 0

Page 12: Arithmetic 1: Prime numbers and factorization …gerin.perso.math.cnrs.fr/Enseignements/Arithmetic1...Arithmetic 1: Prime numbers and factorization (with Solutions) P. Tchebychev (Russia,

<Figure size 1500x1500 with 0 Axes>

N=50000# A good choicem=2**31-1a=7**5b=0x_0=1

# A bad choice:#m=20000#a=177#b=0

Sequence_x=[x_0]CurrentValue=x_0for n in range(1,N+1):

CurrentValue=(a*CurrentValue+b)%mSequence_x.append(CurrentValue)

P=50 # size of the grid

CountingMatrix=np.zeros([P,P])for n in range(N+1)[0:N:2]:

k_n=int(np.floor(P*Sequence_x[n]/m)) # we compute where does x_2i liesell_n=int(np.floor(P*Sequence_x[n+1]/m)) # we compute where does x_{2i+1} liesCountingMatrix[k_n,ell_n]=CountingMatrix[k_n,ell_n]+1

plt.figure(figsize=(15,15), dpi= 100, facecolor='w', edgecolor='k') plt.matshow(CountingMatrix,cmap='hot')plt.colorbar()plt.show()