Social Media restriction in Sri Lanka

Currently in Sri Lanka, there is a restriction against leading social media applications such as Facebook, WhatsApp, Viber, Instagram etc. That does not mean the use of such services are illegal with in the country. There is no law to support it. This is not a ban, but just a restriction temporary imposed by the ISPs based on a request made by the government to control the tens situation arose within the country in last week.

Even though the direct access is restricted, it is possible to access these services through Proxy Servers and VPN (Virtual Private Network) applications. There are some false information, being circulated that the use of either social media services or VPN applications are illegal. There is no ground for such misleading information.

No action can be taken against anyone as long as the services are used in the right manner. Use indirect methods to access these services and use them but do not get involved in spreading racism.

At this moment, it is reported that access of some VPN services are also restricted. But there are plenty of VPN services out there which can be used for the purpose.

Do raise your voice against this restriction !!

Do use social media services responsibly !!


Square and Multiply algorithm

Square and Multiply algorithm is an interesting algorithm which is also known as binary exponentiation algorithm as well. The algorithm is very useful against calculation of large integer powers of a number.

For an example, like calculating; \displaystyle 3^4 (which is multiplying 3, four times), this is pretty straight forward:

3 * 3 = \displaystyle 3^2
3^2 * 3  = \displaystyle 3^3
3^3 * 3  = \displaystyle 3^4

In order to get the answer 3 rounds of calculations are required.

In that same manner, to calculate \displaystyle 5^{13} , 12 rounds of multiplication rounds are required. Nevertheless it is possible to use the following method and reduce number of required rounds.

5^1 * 5^0 =\displaystyle 5^1
5^1 * 5^1 = \displaystyle 5^2
5^2 * 5^2  = \displaystyle 5^4
5^4 * 5^4  = \displaystyle 5^8
5^8 * 5^4  = \displaystyle 5^{12}
5^{12} * 5^1  = \displaystyle 5^{13}

Above is a short cut method to obtain the answer, however things might go weird when the exponent is really large, for example something like \displaystyle 3^{105} or \displaystyle 2^{846} .

Even using the shortcut method seems quite cumbersome but better than the first method.

However, in such situations Square and Multiply method comes handy. Even in the second method, we have applied Square and Multiply operation to get the answer. Using this algorithm make it easier to apply the logic behind, especially when it needs to be implemented as a computer program.

Taking back the earlier example, check the operations carried out:

5^1 * 5^0 =\displaystyle 5^1
(Square) 5^1 * 5^1 = \displaystyle 5^2
(Square) 5^2 * 5^2  = \displaystyle 5^4
(Square) 5^4 * 5^4  = \displaystyle 5^8
(Multiply) 5^8 * 5^4  = \displaystyle 5^{12}
(Multiply) 5^{12} * 5^1  = \displaystyle 5^{13}

what if the above is arranged as follow:

5^1 * 5^0 =\displaystyle 5^1
(Square) 5^1 * 5^1 = \displaystyle 5^2
(Multiply) 5^2 * 5^1  = \displaystyle 5^3
(Square) 5^3 * 5^3  = \displaystyle 5^6
(Square) 5^6 * 5^6  = \displaystyle 5^{12}
(Multiply) 5^{12} * 5^1  = \displaystyle 5^{13}

Even though that there is no much improvement in computational rounds, the second approach aligns with the Square and Multiple method. So as you might already have understood, the Square and Multiple method determines the combination of operations that needs to be carried out to reach to the final answer.


Following steps needs to be carried out :

  1. Get the binary representation of the exponent.
  2. Bits are read from left to right (MSB first) and it should start with a 1.
  3. Starting value\displaystyle n^0
  4. Start scanning bits from the left.
  5. As mentioned above the first bit must be 1.
  6. If scanned bit is 1 then, square the value and then multiply by \displaystyle n
  7. If scanneed bit is 0 then, square the value.
  8. Repeat this for all the bits.

As an example, \displaystyle 5^{13} can be taken. Binary representation of 13 is: 13_{ten}  = \displaystyle 1101_{two}

Since the first bit is 1, initially the value is squared and multiplied. Then next bit is taken, it is 1, therefore the value is square and multiplied again.  Then the third bit is 0, therefore the value is only squared. Final bit is 1, the value needs to be squared and multiplied. Pretty easy right !

Time to check this method for a larger exponent. Lets take following : \displaystyle 3^{105}

\displaystyle 105_{ten} = 1101001_{two}

(Square  ) 3^0 * 3^0 =\displaystyle 3^0
(Multiply) 3^0 * 3^1 =\displaystyle 3^1
(Square ) 3^1 * 3^1 =\displaystyle 3^2
(Multiply) 3^2 * 3^1 =\displaystyle 3^3
(Square ) 3^3 * 3^3 =\displaystyle 3^6
(Square ) 3^6 * 3^6 =\displaystyle 3^{12}
(Multiply) 3^{12} * 3^1 =\displaystyle 3^{13}
(Square ) 3^{13} * 3^{13} =\displaystyle 3^{26}
(Square ) 3^{26} * 3^{26} =\displaystyle 3^{52}
(Square ) 3^{52} * 3^{52} =\displaystyle 3^{104}
(Multiply) 3^{104} * 3^1 =\displaystyle 3^{105}

Python Implementation

Following is the python code:

string bin(int num)

The above function takes an integer as the argument and then return the binary equivalent as a string. It starts with 0b prefix. Therefore it is required to consider [2:] of the string. As the first bit is always 1, the final output of the first step is always equal to x. That is the reason for starting the for loop from 3.

def exp_func(x, y):
    exp = bin(y)
    value = x

    for i in range(3, len(exp)):
        value = value * value
            value = value*x
    return value


Square and Multiply algorithm is a very useful algorithm which can be used to calculate values of integers having really large exponents. The number of calculation rounds is relatively less compared to the brute force method.

Greatest Common Divisor (GDC)

The Greatest Common Divisor (GCD) or Greatest Common Factor is the largest integer which is a common factor of  two or more integers. In other terms, GCD is the largest positive integer that divides each given integer.

For an example, GCD of 20 and 12 is 4. GCD of 18 and 12 is 6. Further, since primes dont have factors (other than 1 and the prime itself) the GCD of any two prime numbers is 1. There is no common factors for 20 and 11, therefore GCD for 20 and 11 is also 1. GCD is not limited to 2 integers, it can be for more than two. For an example, GCD of 30,60 and 90 is 30.

In order to calculate GCD of two numbers following algorithms can be used:

General Method

function cal_gcd(x, y)
Input: Let x and y are two integers

d = min(x,y)
while (True)
    if(x mod d is zero) and (y mod d is zero)
        return d
        d = d - 1

Above algorithm can be used to calculate GCD of two numbers. With a slight modification, the code snippet can be changed so that it calculate GCD for more than two integers.

function cal_gcd_(list)
Input: list of integers
d = min (list)
while (True)
    if remainder = 0 for all integers in the list when divided by d
        d = d -1
        return d

These two algorithms perform better, however once the integers are quite large, these algorithms are not efficient. For large integers, Euclid’s algorithm can be used. Python implementation of the above method mentioned below.

Python Code:

def gcd_gen_list(list_num):
    d = min(list_num)
    cal = True
        counter = len(list_num)
        for i in range(0, len(list_num)):
            if (list_num[i] % d != 0) :
                d -= 1
                counter -= 1
        if((d==1) or (counter == 0)):
            return d

Euclid’s algorithm

This is a pretty interesting method. This method is based on the divisibility concept.

Let, x is divided by y, so that the result is k and remainder is r. In such a case it can be expressed as;

x = k . y + r

Plus this method uses the following theorem.

GCD of integer x and y is equal to GCD of integer y and r.

Repeating this concept till remainder reaches zero.

Pseudocode algorithm:

function gcd_euc
Input: Let x and y are integers

calculate r (r = x mod y)
while (r is not 0)
   calculate r (r = y mod r)
return r

This method is pretty straight forward and very fast when compared to the method mentioned above for large integers.

Python code:

def gcd_euc(x, y):
    r = x%y
    while (r != 0):
        x = y
        y = r
        r = x%y
    return int(y)

Dijkstras Algorithm

This is another algorithm which can be used to compute GCD of two integers. It uses subtraction of numbers to calculate the GCD.

Pseudocode algorithm:

function gcd_dijk
Input: Let x and y are integers

while (x != y)
   If x > y Then
       x = x - y
       y = y - x
return x

Python code:

def gcd_dijk(x, y):
            x = x-y
            y = y-x
    return x

Binary method

The Binary method also known as Stein’s algorithm is also one of the algorithms to compute GCD. It is quite more complex than Dijkstras Algorithm. However unlike
Dijkstras Algorithm it uses division by 2, therefore it is possible to use bit operations.

Pseudocode algorithm:

function gcd_binary
Input: Let x and y are integers

while (x != y)
   factor = 1

   If x is 0 Then
       return factor * y
   If y is 0 Then
       return factor * x

   If x is even AND y is even Then
       factor = factor * 2
       x = x / 2
       y = y / 2

   If x is even AND y is not even Then
       x = x / 2

   If x is not even and y is even Then
       y = y / 2

   If x is not even AND y is not even Then
       If x > y Then
           x = (x - y ) /2
           y = (y - x) / 2
    return factor * x

Python code:

Python implementation is provided below. Note that bit-wise operator >> and << have been used instead of multiplication of 2 or division by 2.

def gcd_binary(x, y):

    factor = 1

        if (x==0 or y==0):
            return factor*(x+y)

        if (is_even(x) == False and is_even(y) == False):
                x = (x-y) >> 1
                y = (y-x) >> 1

        if(is_even(x) and is_even(y)):
            x = x >> 1
            y = y >> 1
            factor = factor << 1 continue if(is_even(x)==True and is_even(y)==False): x = x >> 1
            y = y >> 1

    return factor * x

def is_even(x):
    y = x >>1
        return True
        return False

All methods other than the General Method can be used to calculate GCD for 2 integers at once. In order to calculate GCD for more than two integers, it is possible to calculate GCD for pairs and then select the lowest GCD for all the pairs.

Python code:

def gcd_list(numList):
    numListSize = len(numList)
    GCDList = list()
    for i in range(0, numListSize):
        for j in range(0, numListSize):
                ans = gcd_binary(numList[i], numList[j])
    return min(GCDList)

All methods can be found in bmaths library under
bmaths library is released under GNU General Public License v3.0

Local Authority Election 2018 – Parsing Results (Python Script)

The official results of the Local Authority Election 2018 just released via . If any one is looking for a script to parse the results, check the below link.

The direct connection socket to the site has been removed. Therefore follow below steps unless you want to add your own socket to read the site (check my earlier post on Python Socket programming):

  1. Open the relevant result page and save it in your local PC.
  2. Rename the file name to 98.html (or change the file name in the script)
  3. Execute the script.
This script has been released under GNU GENERAL PUBLIC LICENSE (Version 3)

Socket programming in Python

Python, in default offers low-level access to network services as many other languages do. This is often required when developing client-server applications or an application which needs to communicate with another service. Therefore in this article, it is discussed, how socket programming can be achieved in Python.

Low-Level Access

In this approach, python library socket is used. It is a part of the standard Python library, hence no manual installation is required.

Server code

This is a simple server which keeps listening to an incoming traffic from a client and then respond.


import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

HOST = ''
PORT = 30303

s.bind((HOST, PORT))

while True:
   print ('server running --')
   conn, addr = s.accept()
   data = conn.recv(BUFFER_SIZE)
   print ('Got connection from ', data, '-', addr)
   conn.send(b'Thank you for connecting')

Note how the socket object has been initialized and then HOST address and the PORT have been defined. bind( ) method reserves the port for the host. accept( ) method accepts the incoming connection and then output another instance of the socket object as conn and the addr holds the address (and the port) of the incoming request.

When sending data using send( ) method, the content should be a byte-like object. No string objects can be used. That is why b literal is used. Or string.encode( ) can also be used here. The rest is pretty straight forward.

Client Code

Following is the code snippet for the client.
Code is almost as same as the server code other than not having a loop to listen to the incoming traffic. It simply send some data to the server and then receives the output from the server.


import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
HOST = ''
PORT = 30303

s.connect((HOST, PORT))
data = s.recv(1024)
print (data)
s.close ()

The server code needs to be executed first and then the client code.

However, since there is no threads in the server code, only 1 connection can be established with the server at a time.

Requesting a web page

It is possible to use the client code to contact a web server (or any other service) and request a web page. Or even contact a FTP service. The send request needs to be modified according to the protocol.

Following is a simple example on getting a web page using the client code. Localhost is used in the code however the HOST can be replaced with any URL which supports HTTP.


import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
HOST = ''
PORT = 80

s.connect((HOST, PORT))
s.send(b'GET /test/index.php HTTP/1.1\n'
       b'Connection: keep-alive\n'
       b'Upgrade-Insecure-Requests: 1\n'

data = s.recv(1024)
print (data)
s.close ()

Notice that only the send method has been changed to facilitate HTTP protocol. The output is a plain-text including header and body content. Further processing is required.

High-Level Access

For HTTP protocol, httplib module can be used too. However it is not a part of the standard Python library and it is required to get it installed manually. In the same manner, few non-standard libraries are available for FTP (ftplib), SMTP (smtplib), POP3 (poplib) protocols.

This is a simple introduction on socket programming in Python. According to your requirement the code needs to be modified. The intention was to demonstrate how Python can be used for the above mentioned purpose.

Flick – a new time unit for VR

Recently, the Facebook Open Source has announced that they have launched a new time unit called flick[1]. It captures the attention of many tech communities around the world. But why it is that cool? In this post, will discuss how it is derived using simple mathematical concepts.

The video sampling is measured by the frame rate, for general use it is frames fer second or for high speed motions capturing, frames per millisecond. That means number of frames displayed per time unit. 24 fps (frames per second) is one of the most common frame rate for films. That means 24 frames will be captured/ projected in a second, 24 images in a second.

Following video frame rates with 1/1000 subdivisions are considered when deriving this new unit, flick.

24 fps, 25 fps, 30 fps, 48 fps, 50 fps, 60 fps, 90 fps, 100 fps, 120 fps

when converted to numbers, considering 1/1000 sub divisons:

24 000, 25 000, 30 000, 48 000, 50 000, 60 000, 90 000, 100 000, 120 000

Then not only video, audio is also sampled. In the following manner.

8 kHz, 16 kHz, 22.05 kHz, 24 kHz, 32 kHz, 44.1 kHz, 48 kHz, 88.2 kHz, 96 kHz, 192 kHz

When converted into numbers.

8 000, 16 000, 22 050, 24 000, 32 000, 44 100, 48 000, 88 200, 96 000, 192 000

The new time unit is chosen in a such a way that the above rates can be represented with no fractions, the result is a full integer value. It can be noticed that it is inevitable getting fractions even a nano-second is used as the smallest time unit.

If the new value is f then following equation should be the new representation:

flick value representation = f / (video or audio sample rate)

To find out f, Least Common Multiple (LCM) should be calculated in following numbers.

24 000, 25 000, 30 000, 48 000, 50 000, 60 000, 90 000, 100 000, 120 000, 8 000, 16 000, 22 050, 24 000, 32 000, 44 100, 48 000, 88 200, 96 000, 192 000

However, it can be noticed at once, that the number list can be reduced before applying LCM. Because 48 is a multiple of 24, 90 is multiple of 30, therefore the list reduces as follow :(eliminate smaller number which is factor of a larger number)

90 000, 100 000, 120 000, 88 200, 192 000

then after, it is possible to prime factorize above numbers: (you may use the following calculator[3])

90 000 = 2 x 2 x 2 x 2 x 3 x 3 x 5 x 5 x 5 x 5 = 2^4 x 3^2 x 5^4
100 000 = 2 x 2 x 2 x 2 x 2 x 5 x 5 x 5 x 5 x 5 = 2^5 x (5^5)
120 000 = 2 x 2 x 2 x 3 x 5 = 2^3 x 3 x 5
88 200 = 2 x 2 x 2 x 3 x 3 x 5 x 5 x 7 x 7 = 2^3 x (3^2) x 5^2 x (7^2)
192 000 = 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 3 x 5 x 5 x 5 = (2^9) x 3 x 5^3

According to the method, the next step is to extract higher power primes.

= 2^9 x 3^2 x 5^5 x 7^2
= 512 x 9 x 3125 x 49
f = 705 600 000

If a flick is defined as one, 705 600 000th of a second, all above mentioned rates can be divided without getting any fractions as the result. The result is an integer always.

Further, a flick is nearly 1.4 times larger than a nano-second.

f = (1/ 705 600 000) second
f = 1.41723356 e-9 second

A video with a common rates such as 24 fps (video sampling rate) and 44.1 kHz (audio sampling rate) can now be expressed as 29 400 000 flicks (video rate) and 16 000 flicks (audio rate). Or even it can be denoted 29.4 mega-flicks and 16 kilo-flicks. (could not find any reference for using mega or kilo with flicks anyway)

That is how, the new unit, flick enables conversion of rates without any lose of information of the video sampling or the audio sampling. That is why it becomes a cool representation. C++ implementation can be found in GitHub[2]


Get Free Air Tickets [Hoax]

These days, getting free stuffs from businesses is not an uncommon thing, even from airlines. There are plenty of offers out there. One of my friends has forwarded the following message in WhatsApp.


Free 2 tickets, wow ! I could not wait till I finished reading the message. Because it is all about 2 free tickets from the national carrier. This is unbelievable. So I did check the URL, first it seems that the URL is as same as the official URL of the carrier which is Everything seems pretty cool. So I read it again and checked the URL. Then it flipped. The URL is srilanká , notice the letter á, instead of a.

If you visit the website, it looks as follow. It looks real, almost real, even the logo is also there.


You need to provide some answers, basically YES and NO for 4 questions and congratulations, you secured 2 free tickets. In the next screen, you need to send this to your WhatsApp contacts. Then after you can claim your 2 free tickets.


This URL wont work for desktop browsers, it keeps saying that the offer is not valid for the region while it gets directed to the following URL The reason is pretty simple because it targets the mobile users as it needs WhatsApp to spread the news.

This is not a very smart phishing attack though it is mediocre. The attacker has matched the URL, it is so hard to notice the difference. The web site looks real too. However the Facebook section is not working at all. The connection is not secured (https). This is why you need to check for https always, make it a habit.

Although the offer of 2 free tickets raises your eyebrows, at the same time, it rings a bell. Because it is hard to believe that an airline is just throwing 2 free tickets for a simple survey like this. It would feel real, if it was like 50% for tickets or somewhat.

Finally, to avoid such malicious hoax, make sure you check the URL before you click it. Then look for https, a secure connection. Think before you share anything, specially when you are asked to share something via facebook, WhatsApp, Viber and etc. Last not the least, make sure you use your common sense too.

EDIT : Having https doesnt prove the authenticity. But not having https should ring a bell. So dont get confused. Checking for https (SSL EV) would be  good test.

Did you get the same message? If so what did you do?