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

How to roll a Dice by tossing a Coin ? Cheenta Statistics Department

How can you roll a dice by tossing a coin? Can you use your probability knowledge? Use your conditioning skills.

Suppose, you have gone to a picnic with your friends. You have planned to play the physical version of the Snake and Ladder game. You found out that you have lost your dice.

The shit just became real!

Now, you have an unbiased coin in your wallet / purse. You know Probability.

Aapna Time Aayega

starts playing in the background. :p

Can you simulate the dice from the coin?

Ofcourse, you know chances better than others. :3

Take a coin.

Toss it 3 times. Record the outcomes.

HHH = Number 1

HHT = Number 2

HTH = Number 3

HTT = Number 4

THH = Number 5

THT = Number 6

TTH = Reject it, don't ccount the toss and toss again

TTT = Reject it, don't ccount the toss and toss again

Voila done!

What is the probability of HHH in this experiment?

Let X be the outcome in the restricted experiment as shown.

How is this experiment is different from the actual experiment?

This experiment is conditioning on the event A = {HHH, HHT, HTH, HTT, THH, THT}.

\(P( X = HHH) = P (X = HHH | X \in A ) = \frac{P (X = HHH)}{P (X \in A)} = \frac{1}{6}\)


Beautiful right?

Can you generalize this idea?

Food for thought

Watch the Video here:

Some Useful Links:

Books for ISI MStat Entrance Exam

How to Prepare for ISI MStat Entrance Exam

ISI MStat and IIT JAM Stat Problems and Solutions

Cheenta Statistics Program for ISI MStat and IIT JAM Stat

Simple Linear Regression - Playlist on YouTube

Logarithms and Equations | AIME I, 2000 | Question 9

Try this beautiful problem from the American Invitational Mathematics Examination, AIME I, 2000 based on Logarithms and Equations.

Logarithms and Equations - AIME I 2000


\(log_{10}(2000xy)-log_{10}xlog_{10}y=4\) and \(log_{10}(2yz)-(log_{10}y)(log_{10}z)=1\) and \(log_{10}(zx)-(log_{10}z)(log_{10}x)=0\) has two solutions \((x_{1},y_{1},z_{1}) and (x_{2},y_{2},z_{2})\) find \(y_{1}+y_{2}\).

  • is 905
  • is 25
  • is 840
  • cannot be determined from the given information

Key Concepts


Logarithms

Theory of Equations

Number Theory

Check the Answer


Answer: is 25.

AIME I, 2000, Question 9

Polynomials by Barbeau

Try with Hints


Rearranging equations we get \(-logxlogy+logx+logy-1=3-log2000\) and \(-logylogz+logy+logz-1=-log2\) and \(-logxlogz+logx+logz-1=-1\)

taking p, q, r as logx, logy and logz, \((p-1)(q-1)=log2\) and \((q-1)(r-1)=log2\) and \( (p-1)(r-1)=1\) which is first system of equations and multiplying the first three equations of the first system gives \((p-1)^{2}(q-1)^{2}(r-1)^{2}=(log 2)^{2}\) gives \((p-1)(q-1)(r-1)=+-(log2)\) which is second equation

from both equations (q-1)=+-(log2) gives (logy)+-(log2)=1 gives \(y_{1}=20\),\(y_{2}=5\) then \(y_{1}+y_{2}=25\).

Subscribe to Cheenta at Youtube


Sum of divisors and Integers | TOMATO B.Stat Objective 99

Try this problem from I.S.I. B.Stat Entrance Objective Problem based on Sum of divisors and Integers.

Sum of divisors and Integers (B.Stat Objective Question)

The sum of all positive divisors of 1800, where 1 and 1800 are also considered as divisors of 1800, is

  • 104
  • 6045
  • 1154
  • none of these

Key Concepts


Integers

Sum of divisors

Exponents

Check the Answer


Answer: 6045

B.Stat Objective Problem 99

Challenges and Thrills of Pre-College Mathematics by University Press

Try with Hints


here 1800=(2)(2)(2)(3)(3)(5)(5) where n=\((p_1^a)(p_2^b)(p_3^c)\)

sum of divisors of 1800

=\((\frac{2^{4}-1}{2-1})\)\((\frac{3^{3}-1}{3-1})\)\((\frac{5^{3}-1}{5-1})\) where sum of divisors of n=\((\frac{p_1^{a+1}-1}{p_1-1})(\frac{p_2^{b+1}-1}{p_2-1})(\frac{p_3^{c+1}-1}{p_3-1})\)

=6045.

Subscribe to Cheenta at Youtube


Divisibility and Integers | TOMATO B.Stat Objective 89

Try this problem from I.S.I. B.Stat Entrance Objective Problem based on Integers and divisibility.

Divisibility and Integers (B.Stat Objective Question )


300 digit number with all digits equal to 1 is

  • divisible neither by 37 nor by 101
  • divisible by both 37 and 101
  • divisible by 37 and not by 101
  • divisible by 101 and not by37

Key Concepts


Integers

Remainders

Divisibility

Check the Answer


Answer: divisible by 37 and 101

B.Stat Objective Problem 89

Challenges and Thrills of Pre-College Mathematics by University Press

Try with Hints


here we take 300 digit number all digit 1s

111...11=\(\frac{999...99}{9}\)(300 digits)

=\(\frac{10^{300}-1}{9}\)=\(\frac{(10^{3})^{100}-1}{9}\)=\(\frac{(10^{3}-1)X}{9}\)

since \(10^{3}-1\)=999 is divisible by 37 then 111...11(300 digits) is divisible by 37

111...11=\(\frac{999...99}{9}\)(300 digits)

=\(\frac{10^{300}-1}{9}\)=\(\frac{(10^{4})^{75}-1}{9}\)=\(\frac{(10^{4}-1)Y}{9}\)

since \(10^{4}-1\)=9999 is divisible by 101 then 111...11(300 digits) is divisible by 101.

Subscribe to Cheenta at Youtube


Sets and Probability | B.Stat Objective Problems

Try this problem from I.S.I. B.Stat Entrance Objective Problem from TOMATO based on Sets and Probability. You may use sequential hints to solve the problem.

Sets and Probability (B.Stat Objective problems)


Sixty students appeared in a test consisting of three papers I ,II, and III. Of these students, 25 passed in paper I, 20 in paper II and 8 in paper III. Further 42 students passed in at least one of papers I and II, 30 in at least one of papers I and III, 25 in at least one of papers II and III. Only one student passed in all the three papers. Then the number of students who failed in all the three papers is

  • 17
  • 15
  • 45
  • 33

Key Concepts


Sets

Probability

Algebra

Check the Answer


Answer: 15

B.Stat Objective Problem

Challenges and Thrills of Pre-College Mathematics by University Press

Try with Hints


Let failed P I=P(A)=35, p II=P(B)=40, p III =P(C)=52, P (IandII)=\(P(A \bigcap B)\)= 18 P (I and III)=\(P(A \bigcap C)\)=30 P (II and III)=\(P(B \bigcap C)\)=35 P (I or II or III)=\(P(A \bigcup B \bigcup C)\)=59

Then by Poincare Theorem, \(P(A \bigcup B \bigcup C)\)=P(A)+P(B)+P(C)-\(P(A \bigcap B)\)-\(P(A \bigcap C)\)-\(P(B \bigcap C)\)+x

Then 59=35+40+52-18-30-35+x where x is the required value

then x=59-44=15.

Subscribe to Cheenta at Youtube


Number Series | B.Stat Objective Problem

Problem - Number Series ( B.Stat Objective Problem )


We are going to discuss about Number Series from B.Stat Objective Problem .

A student studying the weather for d days observed that(i) it rained on 7 days morning or afternoon, (ii) when it rained in the afternoon it was clear in the morning, (iii) there were five clear afternoons (iv) there were six clear mornings. Then d equals

  • 8
  • 9
  • 11
  • 10

Key Concepts


Series

Algebra

Integers

Check the Answer


Answer: 9

B.Stat Objective Problem

Challenges and Thrills of Pre-College Mathematics by University Press

Try with Hints


Here A1, A2, A3, A4, A5, A6, A7, A8, A9 are mornings of day 1, day 2, day 3, day 4, day 5, day 6, day 7, day 8 and day 9 again B1, B2, B3, B4, B5, B6, B7, B8, B9 are afternoons of day 1, day 2, day 3, day 4, day 5, day 6 and day 7, day 8 and day 9

A1 A2 clear morning B1 B2 rainy afternoon from first two conditions

A3 rainy A4 clear A5 rainy A6 clear A7 rainy and B3 clear B4 rainy B5 clear B6 rainy B7 clear from first two conditions

So, from first 7 days, 4 clear mornings and 3 clear afternoons. Then A8 clear A9 clear B8 clear B9 clear from last two conditions .Then all condition satisfied that is 4+2=6 mornings clear 3+2 =5 afternoons clear. d=9 that is in 9 days.

Subscribe to Cheenta at Youtube


Numbers and Group | B.Stat Objective Problem

Problem - Numbers and Group (B.Stat Objective Problem)


We are going to discuss about Numbers and Group from B.Stat Objective Problem .

In a group of 120 persons there are 80 Bengalis, 40 Gujratis, Further 71 persons in the group are Muslims and the remaining Hindus, Then the number of Bengali Muslims in the group is

  • 8
  • more than 30
  • 11
  • 10

Key Concepts


Number Series

Algebra

Integers

Check the Answer


Answer: more than 30

B.Stat Objective Problem

Try with Hints


Assuming that out of 80 Bengalis, when 49 are Hindus then 31 are Muslims and when 9 are Hindus 71 are Muslims

Out of 71 Muslims when 40 are Gujratis 31 are Bengalis else 71 are Bengalis which contradicts with other options

Then number of Bengali Muslims are from 31 to 71 that is more than 30.

Subscribe to Cheenta at Youtube


Number of divisors and Integer | B.Stat Objective | TOMATO 83

Try this TOMATO problem from I.S.I. B.Stat Entrance Objective Problem based on Number of divisors and Integer.

Number of divisors and Integer (B.Stat Objective)


The smallest positive integer n with 24 divisors (where 1 and n are also considered divisors of n) is

  • 240
  • 360
  • 420
  • 480

Key Concepts


Number of divisors

Integer

Least positive integer

Check the Answer


Answer: 360

B.Stat Objective Question 83

Challenges and Thrills of Pre-College Mathematics by University Press

Try with Hints


number of divisors of 420=(2+1)(1+1)(1+1)(1+1)=24

number of divisors of 240=(4+1)(1+1)(1+1)=20

number of divisors of 360=(3+1)(2+1)(1+1)=24 and number of divisors of 480=(5+1)(1+1)(1+1)=24 then required number =360.

Subscribe to Cheenta at Youtube


Combinatorics and Integers | TOMATO B.Stat Objective 93

Try this problem from I.S.I. B.Stat Entrance Objective Problem based on Combinatorics and Integers.

Combinatorics and Integers (B.Stat Objective Question)


The highest power of 18 contained in \({50 \choose 25}\) is

  • 104
  • 1
  • 1154
  • none of these

Key Concepts


Integers

Combinatorics

Exponents

Check the Answer


Answer: 1

B.Stat Objective Problem 93

Challenges and Thrills of Pre-College Mathematics by University Press

Try with Hints


here \({50 \choose 25}\)=\(\frac{50!}{(25!)^{2}}\)=\(\frac{(50)(49)(....)(26)}{(25)(24)(...)(1)}\)

\(=(2)^{13}(49)(47)(45)(43)(41)(39)(37)(35)(33)(31)(29)(27) \times \frac{1}{12!}\)

\(=(2)^{10}(49)(47)(15)(43)(41)(13)(37)(35)(11)(31)(29)\times \frac{1}{(12)(11)(10)(9)(8)(7)(6)(5)(4)(3)(2)(1)}[(2)^{3}(27)(9)(3)]\)

\(=(2)^{10}(49)(47)(15)(43)(41)(13)(37)(35)(11)(31)(29)\times \frac{1}{(12)(11)(10)(8)(7)(5)(4)(1)}[(2)(9)]\)gives a factor of \((18)^{1}\) then highest power of 18 is 1.

Subscribe to Cheenta at Youtube