Archive for the ‘Uncategorized’ Category

Many of us would have encountered this situation where we need to run some script which takes hours together to complete.

And most of the time the shell goes inactive and it becomes really hard to track what has happened.

One simple solution for that is to run the script in background.\

For example:

./ &

Oh.. But where are my logs.. Yes, its difficult for us to track what has happened, unless we redirect our output to some specific file .

Anyhow, we have a posix command in linux which can do this stuff. Its nohub.

Checkout the wikipedia link for it here

all you need is to add nohup parameter before your script


Even if the shell goes down, doesnt matter.. We will have the process running in the background. And we can track the logs from nohub.out file . This file will be autocreated in the directory where we ran the script.

For installing python releated packages we need to use pip. Hence installing pip in our server is our first task.

the package name for pip is “python-pip”. Hence the straight forward approach to install pip would be

apt-get install python-pip

But, if the source list is not updated properly , we will end up with older version of pip (in my case i got 1.5.3 however, 8.1.1 is the latest one). Hence to avoid this issue , we need to follow the approach given in wikipedia

i.e  to download the package from internet via this url . Hence we use the curl command


Once gets downloaded. run the python file to install pip


Even beyond this, if we need to install pip package without internet .

Download the source from


extract the folder

tar -xvzf pip-8.1.1.tar.gz

Move inside the folder and install it

cd pip-8.1.1; python install

Square Root Algorithm

Posted: March 17, 2016 in Uncategorized

There are N number of ways to compute square root of a number

we can use the existing library (math) in python to compute the square root

However, there is a constrain in number of decimal digits . Hence, we need an algorithm which can compute a square root and provide n decimal digits .

The following algorithm uses Digit by digit calculation from this wikipedia link

Square Root Computation Algorithms

I have created a python script which will compute square root of 2 with 100 decimal digits .

And i verified the digits from this link

First 100 Digits for SQRT of 2 – Verification

The algorithm is not optimized. However, it gives me the result quickly for first 100 digits

For multiple irrational number , populate the irr list (by default contains 2)

To control number of digits after decimal change decimal variable (by default set to 100)

<br data-mce-bogus="1">
import time
import math

starttime = time.time()

global flag, start, end, decimal
flag = True
start = 0
end = 1
decimal = 100

def main():

answer = 0
irr = [2]

# for each irrational number
for num in irr:

# Step - 1 : Converting number to string and appending 200 zeros
str_num = str(num)
for i in range(0, 210):
str_num = str_num + "0"

# Step - 2: Setting Flag : if lenght of the number is odd we need to take one digit or else two digits
if len(str_num) % 2 != 0:
flag = True
flag = False

start = 0
end = 0
ap_new_dig = 0

# Step - 3: Logic to take one or 2 digits
if flag:
start = 0
end = 1

start = 0
end = 2

temp_num = str_num[start:end]
temp_int_num = int(temp_num)

# Step - 4 : Calculate the initial quotiend and remainder

quo = int(math.sqrt(temp_int_num))
rem = temp_int_num - int(quo ** 2)

# Step - 5 : Here is the main logic to iterate the steps
counter = 0
while counter < decimal - 1 :

# Step - 6 : Bring down the digits
if flag:
start = start + 1
end = start + 2
flag = False
start = start + 2
end = start + 2
new_dig = str_num[start:end]

# Step -7 : Add the digits with previous remainder
ap_new_dig = str(rem) + new_dig

# Step - 8 : Compute a number less than max ap_new_digit
double_num = quo * 2
max_val = 0
t = 0
for i in range(1, 10):
if int( str(double_num) + str(i) ) * i <= int(ap_new_dig):
t = int( str(double_num) + str(i) ) * i
max_val = i

# Step - 9 : Find new quotient and remainder
quo = int ( str(quo) + str(max_val) )
rem = int(ap_new_dig) - t

counter = counter + 1

# Final Quotient will be the answer
print "Square Root of N is" , quo


print "Time taken ", time.time() - starttime


Square Root of N is 1414213562373095048801688724209698078569671875376948073176679737990732478462107038850387534327641572
Time taken  0.0269999504089



Learning Year – 2014 !!!!

Posted: December 31, 2014 in Uncategorized

Started of the year with lot of resolutions ….. 



Speed Cubing


In Feb, i entered into a new world called speedcubing. Never heard it before. Never solved a rubik cube before. Feb 18, 2014 was a start. Initially, i just started off with “how to solve rubik cube” video in you tube. When i started practicing it , things started to be serious and fun as well. took me from 8 minutes to 28 seconds. Thanks and salute to badmephisto.


How to Solve Rubik’s Cube Blindfolded



 Everytime, when i see a blindfolded solve in internet , it always looked like a magic. I thought , i would never make it. But, this link made me understand that i was wrong. Memos, formulas and other stuff .. Ufff.. Nov 18, 2014 i got my first rubik blindfold solve.




One of the interesting medium to learn about softwares and related stuff is coursera. Enrolled in many courses , but didnt finish even one with certificate :P.
however, it was a very good learning experience . R programming was new to me. STarted with the basics of data analytics and also some python stuff.




Everytime when i take javascript , i could not move more than 5 pages in book . Jquery changed that interest. Started watching my favorite programmer – Alex’s video in youtube for jquery and completed till 40/200. It was a wonderful start for basics and was very helpful while programming front end stuffs.


Project Euler Problems



 In early december, moved back to that old black console (c++) and started solving problems on project euler. Initially , the problems were easier and simillar. But, when i moved on things were getting complex and there were lot of Project Euler Problemsthinking and learning involved. Got more ideas on sieves, searching, sorting, dynamics and other stuff. solved 50 problems on it and ranked less than 500 in india out of 8k odd people.


Creating Android App



Is that very difficult to create an android app ? Wanted an alarm and remainder app in my own way. And i designed it with online help.

 Learn German





 Duolingo made it as a routine and learnt it for 30 days . Few words in German got set into my mind .

Chess Cube 


One more addictive website.. chesscube.. Hmmm.. what to say.. Restricted myself to one game a day.. Very interesting to play with unknown persons. But, it eats lot of time. Also, went thro some videos of Akobian (French Defense, Ruy Lopez) just to maintain my cubits in chesscube. and also used chess android app level 4. 4/12 was too low .. but i wanted to play level 4 until i gain good confidence on it.



And at last, blogging.. Couldn’t post much.. Thats gonna be my new year resolution. Will try to share as much as i can . !!!



Happy Learning !!!

Setting the size of an Array

Posted: February 7, 2014 in Uncategorized

we need to be very careful while setting the size of an array . Especially, then we use those variables in looping and conditions.

Let us see one example where we are creating a char array and reading the files based on the char array value.

char param[200];
// get input values
// do some stuff

Due to some chage the in param value we have more than 10 fileds to be added in param . even if we are adding 10 entries to param the loop will end up in the infitie execution.

This is because the value in the char arr can store only upto 200 characters. Since we added some more values to param , the size of the param will cross more than 200 . Bcoz of that param.eof will never get matched and our application will be in running in infite loop.

Hence, we need to be careful while allocation memory to a char variable

i was surprised seeing string being accessed like an array.

say , we ve a field named ‘number’ and the value is ‘987654321’.

since we ve declared the field number as char , we can access individual token as below

select number[2] from table

the index 2 Will denote the second character . in our case its ‘8’

Note : array index starts from 0 , but here index starts from 1. if you try number[0], it Will throw an error .

similarly we can also update individual tokens

update tablename set number[2] = 0 where id = 1

now the string Will be

Say we have a file input.txt which contains

File : input.txt

and now, we want to remove duplicate entries from the file . Its easy ! Just a single line in perl

 cat input.txt | perl -ne '{$H{$_}++ or print $_'

This will give you the output

File : input.txt