A spiral of first 10k prime numbers…

A prime number is a natural number greater than 1 that is not a product of two smaller natural numbers.

Euclid proved the infinity of primes by contradiction.

  • Assume there are a finite number, n , of primes , the largest being \(p_n\);
  • Consider the number that is the product of these, plus one: \(N = \prod\limits_{i = 1}^n {{p_i}} + 1\)
  • By construction, \(N\) is not divisible by any of the \(p_i\).
  • Hence it is either prime itself, or divisible by another prime greater than \(p_n\), contradicting the assumption.

Euclid’s proof is the easiest to understand, especially if you’re not well-versed in mathematics, but now I am going to introduce another proof of the infinitude of primes, which is shorter and I also find it to be the most appealing, at least in my eyes.

In mathematics, the prime-counting function is the function counting the number of prime numbers less than or equal to some real number \(x\). E.g. for \(x=10.124\), we have that 4 number of prime number: 2, 3, 5, 7.

Let \(\pi(x)\) be a prime-counting function.

The Prime Number Theorem suggests that:

\(\mathop {\lim }\limits_{x \to \infty } \frac{{\pi \left( x \right)}}{{x/\ln (x)}} = 1\)

In other words, we can say that \(\pi(x)\) “behaves” like \(x/\ln (x)\) when \(x\) is approaching infinity — there’s an asymptotic ‘equality’.

So, there are infinite primes.

For Python code, we use SymPy – a Python library for symbolic mathematics. In particular, we’ll use primerange(a, b) for generate all prime numbers in the range [a, b).

The code:

import sympy
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as ani

def convert_to_polar_coordinates(num):
    return num*np.cos(num), num*np.sin(num)

lim = 10000

fig, ax = plt.subplots(figsize = (8, 8))
primes_numbers = sympy.primerange(0, lim)
primes_numbers = np.array(list(primes_numbers))
x, y = convert_to_polar_coordinates(primes_numbers)

def init():
    ax.plot([], [])

def plot_in_polar(i):
    ax.plot(x[:i], y[:i], linestyle='', marker='o', markersize=0.75, c='#FC0000')
    ax.set_xlim(-lim, lim)
    ax.set_ylim(-lim, lim)


animator = ani.FuncAnimation(fig=fig, func=plot_in_polar, interval=1, frames=len(primes_numbers))


A spiral of first 10k prime numbers in a polar coordinate system.

How to generate and read QR Code in Python

QR code is a type of matrix barcode that is machine readable optical label which contains information about the item to which it is attached. In practice, QR codes often contain data for a locator, identifier, or tracker that points to a website or application, etc.

Problem Statement :

Generate and read QR codes in Python using qrcode and OpenCV libraries

Installing required dependencies:

pyqrcode module is a QR code generator. The module automates most of the building process for creating QR codes. This module attempts to follow the QR code standard as closely as possible. The terminology and the encoding used in pyqrcode come directly from the standard.

pip install pyqrcode

Install an additional module pypng to save image in png format:

pip install pypng

Import Libraries

import pyqrcode
import png
from pyqrcode import QRCode
import cv2
import numpy as np

Create QR Code:


# String which represents the QR code
s = "http://www.raucci.net"

# output file name

filename = "qrcode.png"

# Generate QR Code

img = pyqrcode.create (s)

# Create and save the svg file naming "brqr.svg"

img.svg("brqr.svg", scale=8)

# Create and save the svg file naming "brqr.png"

img.png("brqr.png", scale=6)
qr code file named brqr.png

Read QR Code

Here we will be using OpenCV for that, as it is popular and easy to integrate with the webcam or any video.


# read the QRCODE image

img = cv2.imread('brqr.png')

# initialize the cv2 QRCode detector
detector = cv2.QRCodeDetector()

val, pts, st_code=detector.detectAndDecode(img)


Here the output

The output of code