Partition Numbers and a code to generate one in Python

Author: Kazi Abu Rousan

The pure mathematician, like the musician, is a free creator of his world of ordered beauty.

Bertrand Russell

Today we will be discussing one of the most fascinating idea of number theory, which is very simple to understand but very complex to get into. Today we will see how to find the Partition Number of any positive integer $n$.

Like every blog, our main focus will be to write a program to find the partition using python. But today we will use a very special thing, we will be using Math Inspector - A visual programming environment. It uses normal python code, but it can show you visual (block representation) of the code using something called Node Editor. Here is a video to show it's feature.

Beautiful isn't it? Let's start our discussion.

What are Partition Numbers?

In number theory, a Partition of a positive integer $n$, also called an Integer Partition, is a way of writing $n$ as a sum of positive integers (two sums that differ only in the order of their summands are considered the same partition, i.e., $2+4 = 4+ 2$). The partition number of $n$ is written as $p(n)$

As an example let's take the number $5$. It's partitions are;

$$5 = 5 +0\ \ \ \ \\= 4 + 1\ \ \ \ \\ = 3 + 2\ \ \ \ \\= 3 + 1+ 1\ \ \\= 2 +2 + 1\ \ \\= 2+1+1+1 \ \\= 1+1+1+1+1$$

Hence, $p(5) = 7$. Easy right?, Let's see partitions of all numbers from $0$ to $6$.

Note: The partition number of $0$ is taken as $1$.

Partition of numbers from $0$ to $6$.

See how easy it is not understand the meaning of this and you can very easily find the partitions of small numbers by hand. But what about big numbers?, like $p(200)$?

Finding the Partition Number

There is no simple formula for Partition Number. We normally use recursion but if you want just a formula which generate partition number of any integer $n$, then you have to use this horrifying formula:

Scary!!!-- Try using this to find $p(2)$

Just looking at this can give you nightmare. So, are there any other method?, well yes.

Although, there are not any closed form expression for the partition function up until now, but it has both asymptotic expansions(Ramanujan's work) that accurately approximate it and recurrence relations(euler's work) by which it can be calculated exactly.

Generating Function

One method can be to use a generating function. In simple terms we want a polynomial whose coefficient of $x^n$ will give us $p(n)$. It is actually quite easy to find the generating function.

$$ \sum_{n =0}^{\infty} p(n)x^n = \Pi_{j = 0}^{\infty}\sum_{i=0}^{\infty}x^{ji} = \Pi_{j = 1}^{\infty}\frac{1}{1-x^j}$$

This can also written as:

$$ \sum_{n =0}^{\infty} p(n)x^n = \frac{1}{(1-x)}\frac{1}{(1-x^2)}\frac{1}{(1-x^3)}\cdots = (1+x^1+x^2+\cdots+x^r+\cdots) (1+x^2+x^4+\cdots+x^{2r}+\cdots)\cdots $$

Each of these $(1+x^k+x^{2k}+\cdots+x^{rk}+\cdots)$ are called sub-series.

We can use this to find the partition number of any $n$. Let's see an example for $n=7$. There are 2 simple steps.

  1. First write the polynomial such that each sub-series's maximum power is smaller or equals to $n$. As an example, if $n=7$, then the polynomial is $(1+x+x^2+x^3+x^4+x^5+x^6+x^7)(1+x^2+x^4+x^6)(1+x^3+x^6)(1+x^4)(1+x^5)(1+x^6)(1+x^7)$.
  2. Now, we simplify this and find coefficient of $x^n$. In this process we find the partition numbers for all $r<n$.

So, for our example, we can just expand the polynomial given in point-1. You can do that by hand. But I have used sympy library.

from sympy import *
x = symbols('x')
a = expand((1+x+x**2+x**3+x**4+x**5+x**6+x**7)*(1+x**2+x**4+x**6)*(1+x**3+x**6)*(1+x**4)*(1+x**5)*(1+x**6)*(1+x**7))
print(a)
#Output: x**41 + x**40 + 2*x**39 + 3*x**38 + 5*x**37 + 7*x**36 + 11*x**35 + 15*x**34 + 18*x**33 + 24*x**32 + 30*x**31 + 37*x**30 + 44*x**29 + 52*x**28 + 57*x**27 + 64*x**26 + 71*x**25 + 77*x**24 + 81*x**23 + 84*x**22 + 84*x**21 + 84*x**20 + 84*x**19 + 81*x**18 + 77*x**17 + 71*x**16 + 64*x**15 + 57*x**14 + 52*x**13 + 44*x**12 + 37*x**11 + 30*x**10 + 24*x**9 + 18*x**8 + 15*x**7 + 11*x**6 + 7*x**5 + 5*x**4 + 3*x**3 + 2*x**2 + x + 1

The coefficients of $x^k$ gives $p(k)$ correctly up to $k = 7$. So, from this $p(7) = 15$, $p(6)=11$ and so on. But for $n>7$, the coefficients will be less than $p(n)$, as an example $p(8)=22$ but here the coefficient is $18$. To calculate $p(8)$, we need to take sub-series with power $8$ or less.

Although this method is nice, it is quite lengthy. But this method can be modified to generate a recurrence relation.

Euler's Pentagonal Number Theorem

Euler's Pentagonal Theorem gives us a relation to find the polynomial of the product of sub-series. The relation is:

$$ \Pi_{i = 1}^{\infty }(1-x^n) = 1 + \sum_{k =1}^{\infty } (-1)^k \Big(x^{k\frac{(3k+1)}{2}} + x^{k\frac{(3k-1)}{2}}\Big) $$

Using this equation, we get the recurrence relation,

$$p(n) = p(n-1) + p(n-2) - p(n-5) - p(n-7) + \cdots = \sum_{k=1}^{n} \Bigg( p\Bigg(n-\frac{k(3k-1)}{2}\Bigg) + p\Bigg(n-\frac{k(3k+1)}{2}\Bigg)\Bigg) $$

The numbers $0, 1, 2, 5, 7, 12, 15, 22, 26, 35, 40,\cdots $ , are called pentagonal numbers. We generate these numbers by $\frac{k(3k+1)}{2}$ and $\frac{k(3k-1)}{2}$.

Let's try to find $p(7)$, using this.

Using this simple algorithm, we can write a code to get partition number.

def par(n):
	summ = 0
	if n == 0 or n == 1:
		result = 1
	else:
		for k in range(1,n+1):
			d1 = n - int((k*(3*k-1))/2)
			d2 = n - int((k*(3*k+1))/2)
			sign = pow(-1,k+1)
			summ = summ + sign*(par(d1) + par(d2))
		result = summ
	return result

This is the code to generate partition number. This is not that efficient as it uses recurrence. For big numbers it will take huge amount of time. In math-inspector, we can see it's block diagram.

Here the par block represent the function par(n). Whenever i creates a variable $n1=6$ or $n3=10$, it creates a circle. To apply par on $n3$, i just need to connect their nodes. And to show the output, add the other node to output node.

So, we have seen this simple function. Now, let's define one using the euler's formula but in a different manner. To understand the used algorithm watch this: Explanation

def partition(n):
    odd_pos = []; even_pos= []; pos_d = []
    for i in range(1,n+1):
        even_pos.append(i)
        odd_pos.append(2*i+1)
    m = 0; k = 0
    for i in range(n-1):
        if i % 2 == 0:
            pos_d.append(even_pos[m])
            m += 1
        else:
            pos_d.append(odd_pos[k])
            k += 1

    initial = 1; pos_index = [initial]; count = 1
    for i in pos_d:
        d = initial + i
        pos_index.append(d)
        initial = d
        count += 1
        if count > n:
            break
    sign = []
    for i in range(n):
        if i % 4 == 2 or i % 4 == 3:
            sign.append(-1)
        else:
            sign.append(1)
    pos_sign = []; k = 0
    for i in range(1,n+1):
        if i in pos_index:
            ps = (i,sign[k])
            k = k + 1
            pos_sign.append(ps)
        else:
            pos_sign.append(0)
    if len(pos_sign) != n:
        print("Error in position and sign list.")
    partition = [1]
    f_pos = []
    for i in range(n):
        if pos_sign[i]:
            f_pos.append(pos_sign[i])
        partition.append(sum(partition[-p]*s for p,s in f_pos))
    return partition

This is the code. This is very efficient. Let's try running this function.

Note: This function returns a list which contains all $p(n)$ in the range $0$ to $n$.

As you can see in the program, In[3] gives a list $[p(0),p(1),p(2),p(3),p(4),p(5)]$. Hence, to get the $p(n)$ using this function, just add $[-1]$ as it gives the last element of the list.

We can use a simple program to plot the graph.

import matplotlib.pyplot as plt
n = 5
x_vals = [i for i in range(n+1)]
y_vals = partition(n)
plt.grid(color='purple',linestyle='--')
plt.ylabel("Partition Number")
plt.xlabel("Numbers")
plt.step(x_vals,y_vals,c="Black")
plt.savefig("Parti_5.png",bbox_inches = 'tight',dpi = 300)
plt.show()

Output is given below. The output is a step function as it should be.

This is all for today. I hope you have learnt something new. If you find it useful, then share.

A code to find Primes - Sieve of Eratoshenes

To some extent the beauty of number theory seems to be related to the contradiction between the simplicity of the integers and the complicated structure of the primes, their building blocks. This has always attracted people.

A. Knauf from "Number theory, dynamical systems and statistical mechanics" 

This quote is indeed true. If you just think about the simplicity of integers and complexity of primes, it's really horrifying. It's almost like how easy and intuitive it is to analyse big "classical objects" but extremely un-intuitive to discuss about "quatum objects".

Today's blog will be a bit special. Today we will discuss about a simple algorithm which is one of the most easiest algorithm which find primes in a certain range. This is called The Sieve of Eratosthenes.

Main Idea of Eratosthenes

The main idea behind this algorithm is very simple. Let's try to understand this using an example.

Suppose, you want to find all primes within $20$.

This is basically our algorithm. Below you can find the algorithm for $30$.

Sieve of Eratosthenes
Code to perform this

The code to perform this is quite easy, specially in python, because of the filter function. Let's see the code;

n = 20
all_list = [i for i in range(2,n+1)]
factor_2_remove = filter(lambda m: m %2 !=0, all_list)
factor_2_remove = list(factor_2_remove)
factor_2_remove.append(2)
print(factor_2_remove)
#output:[3, 5, 7, 9, 11, 13, 15, 17, 19, 2]

As you can clearly see, first we have created a list containing a list from $2$ to $n$, then we use filter. This function filter out the numbers which doesn't satisfy the given. Here the condition is $m \not\equiv 0$ mod $2$. This is written as $m%2 !=0$ in python. After using this we again have to add $2$ as it also doesn't satisfy the condition so, it also is removed.

So, to find all primes within any range, we simply do this for all number, not just $2$. You can use a simple for loop for this.

n = 100
all_list = [i for i in range(2,n+1)]
for i in len(all_list):
    first_ele = all_list[0]
    all_list = filter(lambda m: m %first_ele !=0, all_list)
    all_list = list(all_list)
    all_list.append(first_ele)
print(all_list)
#Output: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]

See!, how easy it is.. But we can do even better. Notice, here we have to run the loop $\pi(n)$ times. But we don't have to do that.

Note: $\pi(n)$ is a function, which represent number of primes in between $2$ to $n$.

Remember: If an integer $a>1$ is not divisible by any prime $p\leq \sqrt{a}$, then $a$ is of necessity a prime.

Using this we can increase the efficiency of the program. Let's try to understand this using an example.

Suppose, we want to find primes up to $100$. Then, we create a list up to $100$, i.e., $2,3,4,5,6,7,\cdots ,98,99,100$. Now, we take $2$ and remove all multiples of $2$ from the list, except $2$ itself. We again do the same for $3$, $5$,$\cdots $. But notice $\sqrt{100}=10$, so we just do this up to $10$, i.e., up to $7$. And this will generate all primes within $100$.

If we do this, now the we only have to run the loop for $\pi(n)$ times. Let's see how much the speed has increased. I will now make two functions, one which runs the loop $\pi(n)$ times and one which runs $\pi(\sqrt{n})$ times.

import math as mp
def with_just_sqrtn(n):#uses sqrt n
    all_list = [i for i in range(2,n+1)]
    root_n = mp.ceil(mp.sqrt(n))
    for i in range(n):
        first_ele = all_list[0]
        all_list = filter(lambda m: m %first_ele !=0, all_list)
        all_list = list(all_list)
        all_list.append(first_ele)
        if first_ele == root_n:
            break
    return all_list
print(with_just_sqrtn(100))
#output: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]

See, the output is same as before.

The previous one can also be written as a function.

def with_all(n):
    all_list = [i for i in range(2,n+1)]
    for i in range(n):
        first_ele = all_list[0]
        all_list = filter(lambda m: m %first_ele !=0, all_list)
        all_list = list(all_list)
        all_list.append(first_ele)
    return all_list

Now, let's find which one takes much time. We will use $n=1,00,000$

n = 1_00_000
import time
start_t = time.time()
with_all(n)
print("Time = %s seconds"%(time.time()-start_t))
#Output: Time = 131.59189105033875 seconds

Just close to $2$ min $11$ sec. Now, let's run the one with small loop number.

start_t = time.time()
with_just_sqrtn(n)
print("Time = %s seconds"%(time.time()-start_t))
#Output: Time = 0.1107027530670166 seconds

What the !!, just 0.11 sec?

So, i hope now you can see, how much faster it is compared to the previous one.

This is all for today, I hope you have learnt something new.

A simple convergence comparison between Leibniz's and mine pi formula

Author: Kazi Abu Rousan

Here there is $\pi$, there is circle.

Today's blog will be a bit different. This will not discuss any formula or any proof, rather it will just contain a python program to compare a $\pi$ formula given by Leibniz and a one discovered by me (blush).

How does Leibniz formula looks like?

The Leibniz formula for $\pi$ is named after Gottfried Leibniz( published in 1676). Sometimes it is also called Leibniz-Madhava series as this is actually a special case of a more general formula discovered by Madhava of Sangamagrama in 14th century.

The formula is:

$\frac{\pi}{4}$ = $1 - \frac{1}{3}$ + $\frac{1}{5}$ - $\frac{1}{7}$ + $\frac{1}{9}$ -$\cdots$ + $\frac{1}{4n+1}$ - $\frac{1}{4n+3}$ + $\cdots$

If you want a visual proof, read my article : Article- click

or this video: Geometric proof

So, how can you find $\pi$ using this?, Just calculate each fractions and add.

As an example, $4, 4(1-\frac{1}{3}) = 2.6667$, $4(1-\frac{1}{3}$ + $\frac{1}{5}) = 3.4664$, $4(1-\frac{1}{3}$ + $\frac{1}{5}$ - $\frac{1}{7}) =2.895238 $, just like this.

But it is nowhere close to the value of $\pi = 3.141592653\cdots$

The conclusion is that, it converges very slowly. Let's ask a computer to find the value of $\pi$ using the series.

pi_val = 3.1415926535897932#right value upto 17 decimals
calculated_pi_from_series = 0
n = 1000
for i in range(0,n):
    calculated_pi_from_series = calculated_pi_from_series + 1/(4*i+1) - 1/(4*i+3)
calculated_pi_from_series = 4*calculated_pi_from_series
print(calculated_pi_from_series)
print("error = ",pi_val - calculated_pi_from_series)
#output: 3.1410926536210413
#output: error =  0.0004999999687518297

For a output of $3.141592153589724$ and error $= 5.000000689037165e-07$, we need $n$ = $10,00,000$. As you can see slow this series is. But even then it has great mathematical significance. It even has a beautiful geometric relationship with Fermat's two square theorem and prime numbers.

How does my formula looks like?

As it is my own discovery, it doesn't have any name but you may call it Rousan's Formula (🤣🤣). This one is almost identical to Leibniz's formula. It is so as it also uses 2d complex numbers as it's basis just like Leibniz's formula.

The main difference is that it mine uses Eisenstein Integers, i.e., triangular grid while Leibniz's uses Gaussian Integers, i.e., regular square grid.

This formula can be written as :

$\frac{\pi}{3\sqrt{3}}$ = $1 - \frac{1}{2}$ + $\frac{1}{4}$ - $\frac{1}{5}$ + $\frac{1}{7}$ - $\cdots$ + $\frac{1}{3n+1}$ - $\frac{1}{3n+2}$ + $\cdots$

Almost same right?, now let's use python to see how fast it converges.

import math as mp
pi_val = 3.1415926535897932#right value upto 17 decimals
calculated_pi_from_series = 0
n = 1000
for i in range(0,n):
    calculated_pi_from_series = calculated_pi_from_series + 1/(3*i+1) - 1/(3*i+2)
calculated_pi_from_series = 3*calculated_pi_from_series*mp.sqrt(3)
print(calculated_pi_from_series)
print("error = ",pi_val - calculated_pi_from_series)
#output: 3.141015303363362
#output: error =  0.0005773502264312391

For $n = 10,00,000$, we get $3.1415920762392955$ with an error of $5.773504976325228e-07$. So, This shows mine converges a bit more slowly.

A direct comparison and reason

So, why mine converge more slowly?, well it is hidden in it's shape. Mine uses triangular grid and leibniz's uses squares. This is the main reason. But fear not, I have actually found a more generalized version of fermat's two square theorem, by using it i can get another similar series which converges faster than leibniz. Maybe I will discuss that in some later blog.

For now, let's try to plot both series's error. To we will plot the number of terms taken in x axis and error in y axis.

import matplotlib.pyplot as plt
import math as mp
pi_val = 3.1415926535897932
sqrt_3_3 = 3*mp.sqrt(3)

def leib_err(n):
    result = 0
    for i in range(n):
        result = result + 1/(4*i+1) - 1/(4*i+3)
    result = 4*result
    error = pi_val - result
    return result

def rou_err(n):
    result = 0
    for i in range(n):
        result = result + 1/(3*i+1) - 1/(3*i+2)
    result = result*sqrt_3_3
    error = pi_val - result
    return result
n_vals = [i for i in range(10_000+1)]
leibniz = [leib_err(i) for i in n_vals]
# print(leibniz)
rousan  =  [rou_err(i) for i in n_vals]
#
plt.plot(n_vals,leibniz,c='red',label='Leibniz Error')
plt.plot(n_vals,rousan,c='black',label='My formula Error')
plt.legend(loc='best',prop={'size':7})
plt.ylim(3.1375,3.1425)
plt.title("Error vs No. of terms for Leibniz and my $\\pi$ formula.")
plt.show()

The output is:

Error vs no. of terms for Leibniz and my pi formula
Error vs no. of terms for Leibniz and my pi formula - 2

This is all for today, see you guys next time with something new.

Your Personal Mandelbrot Set

Author: Kazi Abu Rousan

Bottomless wonders spring from simple rules, which are repeated without end.

Benoit Mandelbrot

Today, we will be discussing the idea for making a simple Mandelbrot Set using python's Matplotlib. This blog will not show you some crazy color scheme or such. But rather the most simple thing you can make from a simple code. So, Let's begin our discussion by understanding what is a Mandelbrot Set.

What is Mandelbrot Set?

In the most simple terms, The Mandelbrot set is the set of all complex numbers c for which the function $f_c(z) = z^2 + c$ doesn't goes to infinity when iterated from $z=0$.

Let's try to understand it using a example.

Suppose, we have a number $c = 1$, then to $ f_c(z)=f_1(z)= z^2 + 1 $.

Then, we first take $z_1 = f_1(z=0)=1$, after this we again calculate $z_2 = f_1(f_1(0)) = 1^2 + 1 = 2$, and so on. This goes on, i.e, we are getting new z values from the old one using $z_{n+1} = {z_n }^2 + 1$. This generates the series $0,1,2,5,26,\cdots$. As you can see, each element of this series goes to infinity, so the number $1$ is not an element of mandelbrot set.

Similarly, if we take $c=-1$, then the series is $0,-1,0.-1,0,\cdots$. This is a oscillating series and hence it will never grow towards infinity. Hence, the number $-1$ is an element of this set.

I hope this idea is now clear. It should be clear that $c$ can be any imaginary number. There is no restriction that says that $c$ must be real. So, is the number $c=i$ inside the set? Try it!!

Check if a Number is inside Mandelbrot set or not

Let's take a point $c = x+iy = P(x,y)$. So, the first point is $z_0 = 0+i\cdot 0 \to x = 0$ and $y=0$, i.e., we start the iteration from $f_c{z=0}$. Now, if at some point of iteration $|z_n|\geq 2$, then the corresponding $c$ value will not be inside the set. But if $|z|<2$, then corresponding c is inside the set.

Try this idea by hand for new numbers.

Now, let's check few numbers using a simple python code.

import numpy as np
def in_mandel(c,max_ita=1000):
    z = 0
    for i in range(max_ita):
        z = z**2 + c
        if abs(z)>=2:
            print(c," is not inside mandelbrot set upto a check of ",(i+1))
            break
        if i==max_ita-1 and abs(z)<2:
            print(c," is inside mandelbrot set upto a check of ",max_ita)
c1 = 1
in_mandel(c1)
#Output = 1  is not inside mandelbrot set
c2 = -1
in_mandel(c2)
#Output = -1  is inside mandelbrot set upto a check of  1000
c3 = 0.5+0.4j
in_mandel(c3)
#output = (0.5+0.4j)  is not inside mandelbrot set

You see how easy the code is!!, you can try out this geogebra version of you want - Check it here: Geogebra

Plot Mandelbrot set

Now, Let's plot one. We will be using a simple rule.

  1. If any number c is inside the set, we will mark that coordinate in the graph.
  2. If it is not there, we will just leave it.
import matplotlib.pyplot as plt
import numpy as np
max_ita = 1000 # number of interations
div = 1000 # divisions
c = np.linspace(-2,2,div)[:,np.newaxis] + 1j*np.linspace(-2,2,div)[np.newaxis,:]
z = 0 
for n in range(0,max_ita):
      z = z**2 + c
p = c[abs(z) < 2]#omit z which diverge 
plt.scatter(p.real, p.imag, color = "black" )
plt.xlabel("Real")
plt.grid(color='purple',linestyle='--')
plt.ylabel("i (imaginary)")
plt.xlim(-2,2)
plt.ylim(-1.5,1.5)
plt.show()

The output is:

Mandelbrot set
Mandelbrot set

This is not as good as the plots you normally see in internet like the images given below. But this one serves the purpose to show the shape of the set.

Let's try to make a bit fancier one.

The code is given here:

import matplotlib.pyplot as plt
import numpy as np
max_ita = 1000
div = 1000
c = np.linspace(-2,2,div)[:,np.newaxis] + 1j*np.linspace(-2,2,div)[np.newaxis,:] 
ones = np.ones(np.shape(c), np.int)
color = ones * max_ita + 5
z = 0
for n in range(0,max_ita):
      z = z**2 + c
      diverged = np.abs(z)>2
      color[diverged] = np.minimum(color[diverged], ones[diverged]*n)
plt.contourf(c.real, c.imag, color)
plt.xlabel("Real($c$)")
plt.ylabel("Imag($c$)")
plt.xlim(-2,2)
plt.ylim(-1.5,1.5)
plt.show()

Here is the output:

Beautiful isn't it?, This set has many mystery. Like you can find the value of $\pi$ and Fibonacci number from it. It is also hidden in Bifurcation diagram and hence related to chaos theory.

But we will live all these mysteries for some later time. This is all for today. I hope you have enjoyed this and if you have, then don't forget to share.

Collatz Conjecture and a simple program

Author: Kazi Abu Rousan

Mathematics is not yet ready for such problems.

Paul Erdos

Introduction

A problem in maths which is too tempting and seems very easy but is actually a hidden demon is this Collatz Conjecture. This problems seems so easy that it you will be tempted, but remember it is infamous for eating up your time without giving you anything. This problem is so hard that most mathematicians doesn't want to even try it, but to our surprise it is actually very easy to understand. In this article our main goal is to understand the scheme of this conjecture and how can we write a code to apply this algorithm to any number again and again.

Note: If you are curious, the featured image is actually a visual representation of this collatz conjecture \ $3n+1$ conjecture. You can find the code here.

Statement and Some Examples

The statement of this conjecture can be given like this:

Suppose, we have any random positive integer $n$, we now use two operations:

Mathematically, we can write it like this:

$f(n) = \frac{n}{2}$ if $n \equiv 0$ (mod 2)

$ f(n) = (3n+1) $ if $n \equiv 1$ (mod 2)

Now, this conjecture says that no-matter what value of n you choose, you will always get 1 at the end of this operation if you perform it again and again.

Let's take an example, suppose we take $n=12$. As $12$ is an even number, we divide it by $2$. $\frac{12}{2} = 6$, which is again even. So, we again divide it by $2$. This time we get, $\frac{6}{2} = 3$, which is odd, hence, we multiply it by 3 and add $1$, i.e., $3\times 3 + 1 = 10$, which is again even. Hence, repeating this process we get the series $5 \to 16 \to 8 \to 4 \to 2 \to 1$. Seems easy right?,

Let's take again, another number $n=19$. This one gives us $19 \to 58 \to 29 \to 88 \to 44 \to 22 \to 11 \to 34 \to 17 \to 52 \to 26 \to 13 \to 40 \to 20 \to 10 \to 5 \to 16 \to 8 \to 4 \to 2 \to 1$. Again at the end we get $1$.

This time, we get much bigger numbers. There is a nice trick to visualize the high values, the plot which we use to do this is called Hailstone Plot.

Let's take the number $n=27$. For this number, the list is $27 \to 82 \to 41 \to 124 \to 62 \to 31 \to 94 \to 47 \cdots \to 9232 \cdots$. As you can see, for this number it goes almost as high as 9232 but again it falls down to $1$. The hailstone plot for this is given below.

Hailstone for 27. The highest number and with it's iteration number is marked

So, what do you think is the conjecture holds for all number? , no one actually knows. Although, using powerful computers, we have verified this upto $2^{68}$. So, if you are looking for a counterexample, start from 300 quintillion!!

Generating the series for Collatz Conjecture for any number n using python

Let's start with coding part. We will be using numpy library.

import numpy as np
def collatz(n):
	result = np.array([n])
	while n >1:
		# print(n)
		if n%2 == 0:
			n = n/2
		else:
			n = 3*n + 1
		result = np.append(result,n)
	return result

The function above takes any number $n$ as it's argument. Then, it makes an array (think it as a box of numbers). It creates a box and then put $n$ inside it.

  1. Then for $n>1$, it check if $n$ is even or not.
  2. After this, if it is even then $n$ is divided by 2 and the previous value of n is replaced by $\frac{n}{2}$.
  3. And if $n$ is odd, the it's value is replaced by $3n+1$.
  4. For each case, we add the value of new $n$ inside our box of number. This whole process run until we have the value of $n=1$.
  5. Finally, we have the box of all number / series of all number inside the array result.

Let's see the result for $n = 12$.

n = 12
print(collatz(n))
#The output is: [12.  6.  3. 10.  5. 16.  8.  4.  2.  1.]

Similarly, for $n = 19$, the code gives us,

n = 19
print(collatz(n))
#The output is: [19. 58. 29. 88. 44. 22. 11. 34. 17. 52. 26. 13. 40. 20. 10.  5. 16.  8.  4.  2.  1.]

You can try out the code, it will give you the result for any number $n$.

Test this function.

Hailstone Plot

Now let's see how to plot Hailstone plot. Suppose, we have $n = 12$. In the image below, I have shown the series we get from $12$ using the algorithm.

Series and Step for $n=12$.

Now, as shown in the above image, we can assign natural numbers on each one of them as shown. So, Now we have two arraies (boxes), one with the series of numbers generated from $n$ and other one from the step number of the applied algorithm, i.e., series of natural numbers. Now, we can take elements from each series one by one and can generate pairs, i.e., two dimensional coordinates.

As shown in the above image, we can generate the coordinates using the steps as x coordinate and series a y coordinate. Now, if we plot the points, then that will give me Hailstone plot. For $n = 12$, we will get something like the image given below. Here I have simply added each dots with a red line.

The code to generate it is quite easy. We will be using matplotlib. We will just simply plot it and will mark the highest value along with it's corresponding step.

import numpy as np
import matplotlib.pyplot as plt
def collatz(n):
    result = np.array([n])
	while n >1:
		# print(n)
		if n%2 == 0:
			n = n/2
		else:
			n = 3*n + 1
		result = np.append(result,n)
	return result
n = 63728127
y_vals = collatz(n); x_vals = np.arange(1,len(y_vals)+1)
plt.plot(x_vals,y_vals)
x_val_max = np.where(y_vals == max(y_vals))[0][0]+1
plt.text(x_val_max, max(y_vals), '({}, {})'.format(x_val_max, int(max(y_vals))))
plt.grid(color='purple',linestyle='--')
plt.ylabel("Sequence Generated")
plt.xlabel("Number of Iteration")
plt.show()

The output image is given below.

Crazy right?, you can try it out. It's almost feels like some sort of physics thing right!, for some it seems like stock market.

This is all for today. In it's part-2, we will go into much more detail. I hope you have learn something new.

Maybe in the part-2, we will see how to create pattern similar to this using collatz conjecture.

Gaussian Prime Spiral and Its beautiful Patterns

Author: Kazi Abu Rousan

Mathematics is the science of patterns, and nature exploits just about every pattern that there is.

Ian Stewart

Introduction

If you are a math enthusiastic, then you must have seen many mysterious patterns of Prime numbers. They are really great but today, we will explore beautiful patterns of a special type of 2-dimensional primes, called Gaussian Primes. We will focus on a very special pattern of these primes, called the Gaussian Prime Spiral. First, we have understood what those primes are. The main purpose of this article is to show you guys how to utilize programming to analyze beautiful patterns of mathematics. So, I have not given any proof, rather we will only focus on the visualization part.

Gaussian Integers and Primes

We all know about complex numbers right? They are the number of form $z=a+bi$, were $i$ = $\sqrt{-1}$. They simply represent points in our good old coordinate plane. Like $z=a+bi$ represent the point $(a,b)$. This means every point on a 2d graph paper can be represented by a Complex number.

In python, we write $i$ = $\sqrt{-1}$ as $1j$. So, we can write any complex number $z$ = $2 + 3i$ as $z$ = $2+1j*3$. As an example, see the piece of code below.

z = 2 + 1j*3
print(z)

#output is (2+3j)
print(type(z))
#output is <class 'complex'>
#To verify that it indeed gives us complex number, we can see it by product law.
z1 = 2 + 1j*3
z2 = 5 + 1j*2
print(z1*z2)
#output is (4+19j)

To access the real and imaginary components individually, we use the real and imag command. Hence, z.real will give us 2 and z.imag will give us 3. We can use abs command to find the absolute value of any complex number, i.e., abs(z) = $\sqrt{a^2+b^2} = \sqrt{2^2+1^2}= \sqrt{5}$. Here is the example code.

print(z.real)
#Output is 2.0
print(z.imag)
#output is 3.0
print(abs(z))
#Output is 3.605551275463989

If lattice points, i.e., points with integer coordinates (eg, (2,3), (6,13),... etc) on the coordinate plane (like the graph paper you use in your class) are represented as complex numbers, then they are called Gaussian Integers. Like we can write (2,3) as $2+3i$. So, $2+3i$ is a Gaussian Integer.

Gaussian Primes are almost same in $\mathbb{Z}[i]$ as ordinary primes are in $\mathbb{Z_{+}}$, i.e., Gaussian Primes are complex numbers that cannot be further factorized in the field of complex numbers. As an example, $5+3i$ can be factorised as $(1+i)(3-2i)$. So, It is not a gaussian prime. But $3-2i$ is a Gaussian prime as it cannot be further factorized. Likewise, 5 can be factorised as $(2+i)(2-i)$. So it is not a gaussian prime. But we cannot factorize 3. Hence, $3+0i$ is a gaussian prime.

Note: Like, 1 is not a prime in the field of positive Integers, $i$ and also $1$ are not gaussian prime in the field of complex numbers. Also, you can define what a Gaussian Prime is, using the 2 condition given below (which we have used to check if any $a+ib$ is a gaussian prime or not).

Checking if a number is Gaussian prime or not

Now, the question is, How can you check if any given Gaussian Integer is prime or not? Well, you can use try and error. But it's not that great. So, to find if a complex number is gaussian prime or not, we will take the help of Fermat's two-square theorem.

A complex number $z = a + bi$ is a Gaussian prime, if:. As an example, $5+0i$ is not a gaussian prime as although its imaginary component is zero, its real component is not of the form $4n+3$. But $3i$ is a Gaussian prime, as its real component is zero but the imaginary component, 3 is a prime of the form $4n+3$.

  1. One of $|a|$ or $|b|$ is zero and the other one is a prime number of form $4n+3$ for some integer $n\geq 0$. As an example, $5+0i$ is a gaussian prime as although it's imaginary component is zero, it's real component is not of the form $4n+3$. But $3i$ is a gaussian prime, as it's real component is zero and imaginary component, 3 is a prime of form $4n+3$.
  2. If both a and b are non-zero and $(abs(z))^2=a^2+b^2$ is a prime. This prime will be of the form $4n+1$.

Using this simple idea, we can write a python code to check if any given Gaussian integer is Gaussian prime or not. But before that, I should mention that we are going to use 2 libraries of python:

  1. matplotlib $\to$ This one helps us to plot different plots to visualize data. We will use one of it's subpackage (pyplot).
  2. sympy $\to$ This will help us to find if any number is prime or not. You can actually define that yourself too. But here we are interested in gaussian primes, so we will take the function to check prime as granted.

So, the function to check if any number is gaussian prime or not is,

import numpy as np
import matplotlib.pyplot as plt
from sympy import isprime
def gprime(z):#check if z is gaussian prime or not, return true or false
	re = int(abs(z.real)); im = int(abs(z.imag))
	if re == 0 and im == 0:
		return False
	d = (re**2+im**2) 
	if re != 0 and im != 0:
		return isprime(d)
	if re == 0 or im == 0:
		abs_val = int(abs(z))
		if abs_val % 4 == 3:
			return isprime(abs_val)
		else:
			return False

Let's test this function.

print(gprime(1j*3))
#output is True
print(gprime(5))
#output is False
print(gprime(4+1j*5))
#output is True

Let's now plot all the Gaussian primes within a radius of 100. There are many ways to do this, we can first define a function that returns a list containing all Gaussian primes within the radius n, i.e., Gaussian primes, whose absolute value is less than or equals to n. The code can be written like this (exercise: Try to increase the efficiency).

def gaussian_prime_inrange(n):
    gauss_pri = []
    for a in range(-n,n+1):
        for b in range(-n,n+1):
            gaussian_int = a + 1j*b
            if gprime(gaussian_int):
                gauss_pri.append(gaussian_int)
    return gauss_pri

Now, we can create a list containing all Gaussian primes in the range of 100.

gaussain_pri_array = gaussian_prime_inrange(100)
print(gaussain_pri_array)
#Output is [(-100-99j), (-100-89j), (-100-87j), (-100-83j),....., (100+83j), (100+87j), (100+89j), (100+99j)]

To plot all these points, we need to separate the real and imaginary parts. This can be done using the following code.

gaussian_p_real = [x.real for x in gaussain_pri_array]
gaussian_p_imag = [x.imag for x in gaussain_pri_array]

The code to plot this is here (exercise: Play around with parameters to generate a beautiful plot).

plt.axhline(0,color='Black');plt.axvline(0,color='Black') # Draw re and im axis
plt.scatter(gaussian_p_real,gaussian_p_imag,color='red',s=0.4)
plt.xlabel("Re(z)")
plt.ylabel("Im(z)")
plt.title("Gaussian Primes")
plt.savefig("gauss_prime.png", bbox_inches = 'tight',dpi = 300)
plt.show()
Gaussian Prime
Look closely, you can find a pattern... maybe try plotting some more primes

Gaussian Prime Spiral

Now, we are ready to plot the Gaussian prime spiral. I have come across these spirals while reading the book Learning Scientific Programming with Python, 2nd edition, written by Christian Hill. There is a particular problem, which is:

Gaussian Integer and Primes Problem

Although, history is far richer than just solving a simple problem. If you are interested try this article: Stepping to Infinity Along Gaussian Primes by Po-Ru Loh (The American Mathematical Monthly, vol-114, No. 2, Feb 2007, pp. 142-151). But here, we will just focus on the problem. Maybe in the future, I will explain this article in some video.

The plot of the path will be like this:

Gaussisan Primes and Gaussian Spirals

Beautiful!! Isn't it? Before seeing the code, let's try to understand how to draw this by hand. Let's take the initial point as $c_0=3+2i$ and $\Delta c = 1+0i=1$.

  1. For the first step, we don't care if $c_0$ is gaussian prime or not. We just add the step with it, i.e., we add $\Delta c$ with $c_0$. For our case it will give us $c_1=(3+2i)+1=4+2i$.
  2. Then, we check if $c_1$ is a gaussian prime or not. In our case, $c_1=4+2i$ is not a gaussian prime. So, we repeat step-1(i.e., add $\Delta c$ with it). This gives us $c_2=5+2i$. Again we check if $c_2$ is gaussian prime or not. In this case, $c_2$ is a gaussian prime. So, now we have to rotate the direction $90^{\circ}$ towards the left,i.e., anti-clockwise. In complex plane, it is very easy. Just multiply the $\Delta c$ by $i = \sqrt{-1}$ and that will be our new $\Delta c$. For our example, $c_3$ = $c_2+\Delta c$ = $5+2i+(1+0i)\cdot i$ = $5+3i$.
  3. From here, again we follow step-2, until we get the point from where we started with the same $\Delta c$ or you can do it for your required step.

The list of all the complex number we will get for this particular example is:

IndexComplex No.Step Index Complex No. Step
0$3+2i$+17 $2+4i$ -1
1 $4+2i$ +18 $1+4i$ -i
2 $5+2i$ +i9$1+3i$-i
3 $5+3i$ +i10$1+2i$+1
4 $5+4i$ -111$2+2i$+1
5 $4+4i$ -112$3+2i$+i
6 $3+4i$ -113$3+3i$+i
Complex numbers and $\Delta c$'s along Gaussian prime spiral

Note that although $c_{12}$ is the same as $c_0$, as it is a Gaussian prime, the next gaussian integer will be different from $c_1$. This is the case because $\Delta c$ will be different.

To plot this, just take each $c_i$ as coordinate points, and add 2 consecutive points with a line. The path created by the lines is called a Gaussian prime spiral. Here is a hand-drawn plot.

Gaussian Prime Spiral hand-drawn plot

I hope now it is clear how to plot this type of spiral. You can use the same concept for Eisenstein primes, which are also a type of 2D primes to get beautiful patterns (Excercise: Try these out for Eisenstein primes, it will be a little tricky).

We can define our function to find points of the gaussian prime spiral such that it only contains as many numbers as we want. Using that, let's plot the spiral for $c_0 = 3+2i$, which only contains 30 steps.

Gaussian primes, integers and spirals

Here is the code to generate it. Try to analyze it.

def gaussian_spiral(seed, loop_num = 1, del_c = 1, initial_con = True):#Initial condition is actually the fact
#that represnet if you want to get back to the initial number(seed) at the end.
    d = seed; gaussian_primes_y = []; gaussian_primes_x = []
    points_x = [d.real]; points_y = [d.imag]
    if initial_con:
        while True:
            seed += del_c; real = seed.real; imagi = seed.imag
            points_x.append(real); points_y.append(imagi)
            if seed == d:
                break
            if gprime(seed):
                del_c *= 1j
                gaussian_primes_x.append(real); gaussian_primes_y.append(imagi)
    else:
        for i in range(loop_num):
            seed += del_c; real = seed.real; imagi = seed.imag
            points_x.append(real); points_y.append(imagi)
            if gprime(seed):
                del_c *= 1j ;
                gaussian_primes_x.append(real); gaussian_primes_y.append(imagi)
    gauss_p = [gaussian_primes_x,gaussian_primes_y]
    return points_x, points_y, gauss_p

Using this piece of code, we can literally generate any gaussian prime spiral. Like, for the problem of the book, here is the solution code:

seed1 = 5 + 23*1j
plot_x, plot_y, primes= gaussian_spiral(seed1)
loop_no = len(plot_x)-1
plt.ylim(21,96.5)
plt.xlim(-35,35)
plt.axhline(0,color='Black');plt.axvline(0,color='Black')
plt.plot(plot_x,plot_y,label='Gaussian spiral',color='mediumblue')
plt.scatter(primes[0][0],primes[1][0],c='Black',marker='X')#starting point
plt.scatter(primes[0][1::],primes[1][1::],c='Red',marker='*',label='Gaussian primes')
plt.grid(color='purple',linestyle='--')
plt.legend(loc='best',prop={'size':6})
plt.xlabel("Re(z) ; starting point = %s and loop number = %s "%(seed1,loop_no))
plt.ylabel("Im(z)")

plt.savefig("prob_sol.png", bbox_inches = 'tight',dpi = 300)
plt.show()

A few more of these patterns are:

Gaussian Prime and Spiral Pattern

One of the most beautiful patterns is generated for the seed: $277 + 232i$.

Gaussian Primes Spiral Patterns

😳 Am I seeing a Bat doing back-flip?

All the codes for generating these can be found here:

Here is an interactive version. Play around with this: https://zurl.co/Hv3U

Also, you can use python using an android app - Pydroid 3 (which is free)

I have also written this in Julia. Julia is faster than Python. Here you can find Julia's version: https://zurl.co/wETi