Open In Colab

Background

In this article, we will examine some interesting relationships between $\pi$ and prime numbers. This is a special article that I prepared to celebrate the Pi day, which is March 14th.

Probability of integers being relatively prime

We start from a question that is simple to formulate, but difficult to answer:

What is the probability that two integers are relatively prime?

Similar to other articles that I wrote, we will start by using the violent approach, to calculate the probability brute force with the code below:

So after running the program above, it seems that the probability that any two integers are relatively prime is 0.607. We only sampled integers up to 20000 for speed reasons, but you can test other values, and it should yield similar answers.

One concern that some readers may have is that the density of prime numbers will decrease when the maximum range of random sampling becomes larger (for example, there are 4 prime numbers within 10, but only 25 prime numbers within 100). In fact, the density of prime numbers around an integer $x$ is shown to be $1/log(x)$. This does not really impact our findings above since we are only considering relatively prime, not prime by itself. Again you can try different numbers for the minimum and maximum range of integer sampling in the code above and test the program yourself.

Solution by probability multiplication

To calculate the exact probability analytically, we can consider the following scenarios one by one, and then multiply them:

1, given two integer $a$ and $b$, the probability that $a$ can be divided by 2 is 1/2, and the probability that $b$ can be divided by 2 is also 1/2. Therefore, the probability that $a$ and $b$ can both be divided by 2 is (1/2)*(1/2)=1/4. Then the probability that $a$ and $b$ do not have 2 as common divider (or, relatively prime with respect to 2) is 1-1/4.

2, similarly, the probability that $a$ and $b$ do not have 3 as common divider is 1-1/3*1/3=1=1/9.

3, let $p_k$ be the k-th prime number ($p_1$=2, $p_2$=3, $p_3$=5, ...). Then the probability that $a$ and $b$ do not have $p_k$ as common divider is $1-1/p_k*1/p_k=1-1/p_k^2$.

4, then the probability that any two integers are relatively prime is the multiplication of all these probabilities as

$P=\displaystyle\prod_{k=1}^{\infty} (1-1/p_k^2)$

The only difference with the Python code earlier is that we put a maximum boundary of sampling the two numbers ($a$ and $b$), so that the computation can be completed in short period of time. Here there is no upper boundary how much $a$ and $b$ can be.

5, Because Euler Product formula says that

$\displaystyle\prod_{k=1}^{\infty} \frac{1}{1-1/p_k^s} = \displaystyle\sum_{k=1}^{\infty} \frac{1}{n^s} = \zeta(s)$

We have $P = \frac{1}{\zeta(2)} = \frac{6}{\pi^2}$

The Zeta function $\zeta$ is defined in Riemann Hypothesis, which is arguably the most important problem in mathematics and human history. If it is wrong, then the entire infrastructure of modern mathematics will crash. Every week, somebody will publish a paper claiming that they proved the hypothesis, yet none has gained widespread recognition or approval yet. So the hypothesis is better not to be wrong.

So the theoretical answer (0.608) is quite close to our data simulation above.

Since the zeta function is also available in SciPy, we can calculate it and validate its values:

Extension of the problem to m relatively prime integers

In the situation above we considered two integers that are relatively prime. Now we extend the problem to "what is the probability that m integers that are relatively prime". The question is a little tricky, because we only require that the k numbers do not share a common divider, yet it is totally fine that k-1 numbers share a common divider.

Intuitively, since the answer to the qestion above is $1/\zeta(2)$, the answer to this question is likely $1/\zeta(m)$. This is correct.

The same reasoning can be used here. Say for example if we have 3 numbers, and we want to calculate the probability that they are relatively prime with respect to 2 (that is, they do not share a common divider of 2). It is possible that 2 of the 3 numbers are even and the third one is odd. Since the probability that all 3 numbers are even is 1/8, then the probability of the reverse is 1-1/8.

So the final $P=\displaystyle\prod_{k=1}^{\infty} (1-1/p_k^m) = 1/\zeta(m)$

Based on the webpage, the numbers can be reprented as a function of $\pi$ for $m=2$ and $m=4$, but not $m=3$ and other values.

We can use SciPy to calculate the values when m=2, 3, 4. Note that m=2 is a special case of the previous question.

Extension of the problem to n mutually relatively prime integers

This is a slightly different problem than the above one, because we require "mutually relatively prime". This means no two numbers in the $n$ numbers can share a common divider, yet in the previous question it is okay for $n-1$ numbers to share a common divider.

To solve this problem, we still start our thinking from 2 as a divider. Say if we have 3 numbers, to ensure that they are mutually relatively prime, then at most one number can be even, and the other two numbers must be odd. Or all the 3 numbers must be odd. Similarly, if we have 4 numbers, at most one can be even and all other three numbers must be odd.

The general formula therefore seems to be $(1-1/2)^{n}+(1-1/2)^{n-1} n/2$. The first part calculates the probability that all numbers are odd, and the second one calculate the probability that one number is even, and the other ones are odd.

If we then consider the prime number 3 as a divider, we can get similar conclusion that the probability is $(1-1/3)^{n}+(1-1/3)^{n-1} n/3$.

Now consider all prime numbers $p \in \mathbb{p}$. The probability is

$P=\displaystyle\prod_{k=1}^{\infty} ((1-1/p_k)^n+(1-1/p_k)^{n-1}n/p_k)$

It cannot be represented by a zeta function.

The code below tests n=2, n=3 and so on.