Wednesday, January 3, 2018

First python script

Open an editor with first.py

type below lines

print("hi")

print("jith")

Now run it in terminal,

JITSUNDA-M-C2XF:~ jitsunda$ python3 first.py
hi
jith

JITSUNDA-M-C2XF:~ jitsunda$ 

Python interactive mode - REPL

Read–Eval–Print Loop (REPL) is an interactive language shell or interactive computer programming environment. The advantage of having REPL is that the environment receives input from user, evaluates and results in an interactive manner. No need for scripting.


JITSUNDA-M-C2XF:~ jitsunda$ python3
Python 3.6.3 (default, Oct  4 2017, 06:09:15) 
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.37)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

>>> 

I use python3, 

Simply type python3, we will get ">>>" prompt, we can write simple instructions and get the results as shown below



JITSUNDA-M-C2XF:~ jitsunda$ python3
Python 3.6.3 (default, Oct  4 2017, 06:09:15) 
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.37)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print("hi")
hi
>>> 4+3
7
>>> x = "jith"
>>> x
'jith'

>>> 

Intro to Python

Introduction to Python

Python is a computer language similar to C, C++, Java, C# etc .. However there are lot of features in python make it unique compare to other languages. Especially, the unprecedented growth of data science and machine learning is making Python as most popular language at present. This is due to the flexibility and computational friendly nature of Python.

Advantages of Python

Very high level language, so we can prototype rapidly an idea or an algorithm
Object oriented programming, Interpreted, Interactive and Portable
It can interact with C and C++ well (Extensibility feature of Python)
Large set of packages
Easy integration
Apt for scientific computing
Dynamically typed

Limitations of Python

In my opinion, Still it is considered as glue language or just scripting language or language for automating tasks in industry rather than being used as a main-stream language such as Java, C#, C etc ..
Slow compare to C






Sunday, October 11, 2015

pthread

#include "stdio.h"
#include "stdlib.h"
#include "pthread.h"

void thread_run(int m){

    int i = 0;
    while( i < 100 ){
        printf("I am in thread %d\t %d\n", m, i);
        i++;
    }

}

int main()
{
int i;
pthread_t tid;

    for(i = 0; i < 5; i++){
   pthread_create(&tid, NULL, thread_run, i);
    }

pthread_exit(NULL);
return 0;
}

 gcc -lpthread -o pt examp_1.c

Monday, October 21, 2013

Need for 'Pi'

'Pi' the miracle number
-----------------------------


I was asked( long back may be 4 years) to write which known formula provides maximum number of digits to the value of 'pi'. Since my tutor is soft enough, I bravely (rare thing from me :) ) wrote in the paper that "I do not know the formula, but using Ramanujan's formula( which is what he expected, he is a fan/ devotee of great Ramanujan) we can calculate up to some thousands and lakhs of digits, at the same time, in my opinion, the universe radius is in the order of 10 to the power 15 or 20, even if we want to calculate the volume of the universe, it is enough to find around 60 to 70 digits accuracy, I do not know the motivation of finding so many digits for pi, may be this is due to mathematicians' dream/ ego to find out this miracle (maximum number of digits)" !!
Then he explained one of the applications is to test the computation complexity using 'pi' related findings !! 

Now when I saw this article, I remember those days !!


I should have read this article before writing in the paper, but what to do I thought about this after when I read the question !! 


Thursday, October 17, 2013

Sum of cube of first n natural numbers is the square of the sum of first n natural numbers !!

Proof for (Simple Try) !!
Sum of cube of first n natural numbers is the square of the sum of first n natural numbers !!

13+23 + 33+ … +n= (1 + 2 + 3+...+n) 2 

As we know the simple formula
----------------------------------
(a + b) 2 = a2 + 2*ab + b 2

(a + b + c) 2 = a2+ b2+ c 22 (ab + ac 
                                                    + bc)

(a + b + c + d) 2= a2+ b2+c2+ d2
                                                     2(ab + ac + ad 
                                                             + bc + bd 
                                                                     + cd )

The above algebric formula has some pattern ( marked as bold) .. 2 ( a (b+c+d) + b (c+d) + cd))

Now let us look into (1 + 2 … + n) 2

(1 + 2 … + n) 2   = 
             
           12+ 22 + 32 .. + n2 + 2*{1(2+3+4..n) + 2 (3+4+...+n) + ... (n-1)(n)}

             
 (1 + 2 … + n) 2
                             12+ 22 + 32 .. + n2 2*{1*2 + 1*3 + 1*4 + …+ 1*n
                                           + 2*3 + 2*4+ … + 2*n
                                                    + 3*4+…. + 3*n
                                                             +…..+ (n-1) (n)}

Consider the violet terms, this can be written as

2 * 1 + 3 (1+2) + 4 (1+2+3) + …+ (n)(1+2+…(n-1))

using some of first n terms formula ... ( n * (n+1) / 2)) 

The above terms can be rewritten as

2 + 3(2 * 3/2) + 4(3*4/2) + 5(4*5/2) + …. +n((n-1)n/2))

After simplifying, This can be written as

(1 + 2 … + n) 2= 12+ 22 + 32 … + n2 + 2 {2 + 3(2*3/2) + 4 (3*4/3) + ….. n ((n-1) (n/2)}

                          = 12+ 22 + 32 … + n2 + {1*22+2*32 + 3*42….+ (n-1)n2}

                          = 12+ 22 + 32 … + n2 + 22 + 2*32 + 3*4+ ... + (n-1)n

upon rearranging the terms

                           = 12+ (22  22) + (32*32) + … + ((n-1)n2 + n2) 

                            = 12+ (2 * 2) + ( 3 * 32) + … + (n* n2) 

                            =13+ 2 3+ …+ n 3  
                
(1 + 2 + 3 … +n) 2 = 13+ 2 3+ … +n 3          
                                              
Change LHS to RHS and vice –versa

13+ 2 3+ … +n3    =    (1 + 2 + 3 … +n) 2

FROM the induction ( 1 + 2 + 3 … n ) = ( n (n+1) / 2 ) the above equation can be written as

13+ 2 3+ … n3     =    (n (n+1)/ 2) 2