Wednesday 22 February 2017

Change the JAVA_HOME in Ubuntu

Hello All,

Long time break. Sorry for being so. I was equipped with so much new things.
But anyways as we are back, lets begin it from where we left and continue our strive for technology.

A lot of geeks do prefer using open source OS distributions.
And Ubuntu from that perspective is a great choice.

Sometimes i have seen people get stuck with how to set JAVA_HOME in Ubuntu.
Let say you want to change from the previous version 1.7 to 1.8

What would you do in Ubuntu?

Write JAVA_HOME = "Directory for java 1.8" ??
Do you think it will work?

So your plan of action was to
Open Terminal and then
Type JAVA_HOME = "Directory for java 1.8"
And then check the change using echo $JAVA_HOME.

Voila!! It got changed!!

But surprisingly next time you start the system its not gonna work. Oh!! So its valid for the terminal in which you have made the change and its not PERMANENT!!

Yes Geeks!! That's TRUE.

So, now the question is how to make it permanent?
Will go straight to the answer.

Open your terminal(Ctrl+Alt+T)
open environment file in some text editor as change in that file permanently changes the environment variables value. So what to do?
 Type in terminal
It's Very easy to set path in Linux . Do as follows :
Step-1 Open terminal and type sudo gedit .bashrc
Step-2 It will ask you your password . After typing password ,it will open the bash file . Then go to end of the file and type a last line
step-3 export JAVA_HOME= "YOUR JAVA 1.8 DIRECTORY"
step-4 Then save the file and exit from file
And voila!! Its done!!
Check the JAVA_HOME directory now by typing echo $JAVA_HOME

-----------------------------------------------------------------------
Signing off geeks!! :)

Sunday 31 July 2016

Segmented Sieve of Eratosthenes

The classical 'sieve of eratosthenes'(if you don't know this please read it first)
 generates prime no. starting from 1 upto a limit. However if you want to sieve prime numbers between two given numbers, segmented sieve is used. For example if you want prime numbers between say 125 and 150, you need not sieve primes upto 150 and then check how many of them are above 125. The approach is basically to create a bool array of size 25 (150-125) in this case. i-th index of your bool array will correspond to (125+i) number. Then mark non prime no. as follows start from (125/2)*2=124, now begin from 126 (124+2 because you need primes greater than 125 )and remove all no. at a distance of 2, ie 126,128,130...150. () This removes all even no. Then start from (125/3)*3=123,now begin from 126 and remove all elements at distance 3,ie 126,129,132,...150. This removes all multiples of 3. Proceed in similar fashion removing all multiples of prime upto sqrt(150).

To be more generalized, given two no. say m and n, prime no. between m and n can sieved by creating a bool array of size n-m. i th index of the array will correspond to (m+i) no. Sieve all primes by using primes up-to sqrt(n) by doing (m/prime)*prime following the fashion shown in example above.

(Credits: Ayur Jain for above explanation)

Implementation in Java:
public class SegmentedSieve {
public static void main(String[] args) {
    int s=125,e=150;
    int eSqrt = (int)Math.sqrt(e);
    boolean[] b = new boolean[eSqrt+1];
    Arrays.fill(b, true);
    b[0]=b[1]=false;
    for(int i=2;i<=eSqrt;i++){
        if(b[i]){
            for(int j=i*i;j<eSqrt;j+=i)b[j]=false;
        }
    }
    boolean[] segment = new boolean[e-s+1];
    Arrays.fill(segment, true);
    for(int i=2;i<=eSqrt;i++){
        if(b[i]){
            for(int j=((s/i)*i);j<=e;j+=i)if(j>=s)segment[j-s]=false;
        }
    }
   
    for(int i=0;i<segment.length;i++){
        System.out.println((s+i)+":"+segment[i]);
    }
}
}

Wednesday 13 August 2014

Delete the Middle Element from a Linked List and second problem Divide the List around a given value.

class Node{
private int data;
private Node next;
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}

public class LL {
private Node head;private int length;
public static void main(String[] args) {
LL l1 = new LL();
l1.add(12);l1.add(20);l1.add(6);l1.add(12);l1.add(20);l1.add(6);
l1.printList();System.out.println();
Node md = l1.head;
for(int i=0;i<l1.length/2-1;i++){md=md.getNext();}
l1.deleteMiddle(md);System.out.println();
l1.printList();
l1.divideListAround(13);System.out.println();
l1.printList();

}


private Node createNode(int data){
Node newNode = new Node();newNode.setData(data);newNode.setNext(null);return newNode;
}


private void add(int data){ 
length++;
Node nn = createNode(data);
if(this.head==null){ this.head=nn; }
else
{
Node temp = this.head;
while(temp.getNext()!=null)temp=temp.getNext();
temp.setNext(nn);
}
}


private void printList(){
   Node head = this.head;
   while(head!=null){System.out.print(head.getData()+"->");head=head.getNext();}
}

// We assume that reference to the node is given:O(1) time to do that.
private void deleteMiddle(Node mid){
length--;
mid.setData(mid.getNext().getData());
mid.setNext(mid.getNext().getNext());
}


private void divideListAround(int val){
LL left = new LL();
LL right = new LL();
Node h = head;
while(h!=null){
if(h.getData()<val)
left.add(h.getData());
else
right.add(h.getData());
h=h.getNext();
}
//Merge the lists
Node headLeft = left.head;
while(headLeft.getNext()!=null)
{
headLeft=headLeft.getNext();
}
headLeft.setNext(right.head);
this.head=left.head;
}

}

Tuesday 12 August 2014

Remove duplicates from an Unsorted Linked List

//Author: Santanu Naskar//
// Here I have discussed both the methods of removing duplicates: Hash Table approach, Non-buffered //approach......


class Node{
private int data;
private Node next;
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}


public class LL {
private Node head;
public static void main(String[] args) {
LL l1 = new LL();
l1.add(12);l1.add(20);l1.add(6);l1.add(12);l1.add(20);l1.add(6);
l1.printList();System.out.println();
l1.removeDuplicatesWithoutUsingBuffer_O_n2();System.out.println();
l1.printList();System.out.println();
}
private Node createNode(int data){
Node newNode = new Node();newNode.setData(data);newNode.setNext(null);return newNode;
}


private void add(int data){
Node nn = createNode(data);
if(this.head==null){ this.head=nn; }
else
{
Node temp = this.head;
while(temp.getNext()!=null)temp=temp.getNext();
temp.setNext(nn);
}
}


private void printList(){
   Node head = this.head;
   while(head!=null){System.out.print(head.getData()+"->");head=head.getNext();}
}


// Function to remove duplicates from an unsoretd LL //
private void removeDuplicatesUsingHashTable_O_n(){
   java.util.Hashtable<Integer,Integer> ht = new java.util.Hashtable<Integer,Integer>();
   Node current = this.head, previous = null; //current pointing to head and previous set to null
   while(current!=null)
   {
  if(ht.containsKey(current.getData())) // If hashtable doesn't already contain the current.data we need to remove that from the list.
  {previous.setNext(current.getNext());}  // So we set the previous pointer to current's next in order to skip the duplicate value.
  else
  {
  ht.put(current.getData(), 1);  // Put the non-occured value into the hash table for future checking
  previous=current;   // Set previous to node current is pointing now.
  }
 current=current.getNext();
   }
}


private void removeDuplicatesWithoutUsingBuffer_O_n2(){
Node current = head;
while(current!=null){
 Node runner = current;
while(runner.getNext()!=null){   // Its upto you to think why i have not used runner!=null in place of runner.getNext()!=null
if(runner.getNext().getData()==current.getData())
runner.setNext(runner.getNext().getNext());
else
runner=runner.getNext();
 }
current=current.getNext();
}
}
}

Tuesday 29 July 2014

Singly Linked List and All Its Basic Operations in Java

/******@Auhtor: Santanu Naskar ***********/
/****** Linked List & All Its Basic Operations ******/
/******************************************************************/

class Node{
private int data;
private Node next;
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}


public class LinkedList {
static private Node head = null;
private int length = 0;
public static void main(String[] args) {
LinkedList ll = new LinkedList();
ll.put(12);ll.put(121);ll.put(122);
ll.printList();
ll.putAtBeg(20);ll.putAtBeg(40);
ll.printList();
ll.putAt(2, 1000);ll.putAt(4, 1234);ll.putAt(12, 1000);
ll.printList();
System.out.println("length:"+ll.length);
ll.delAtBeg();
ll.printList(); 
ll.deleteAtEnd();ll.deleteAtEnd();
ll.printList();
System.out.println("lgth:"+ll.length);
ll.delAtPos(4);
ll.printList();
}


void delAtBeg(){if(head==null){System.out.println("Nothing to delete");return;};head=head.getNext();this.length--;}


void putAt(int pos,int data){if(pos>this.length+1||pos<1||(head==null && pos!=1)){System.out.println("Invalid position:"+pos);return;}head=this.putInMiddle(head, pos, data);this.length++;}

void put(int data){head=putNewAtEnd(head, data);this.length++;}


void putAtBeg(int data){head=putAtBeginning(head, data);this.length++;}


void delAtPos(int pos)
{
if(head==null||pos>length+1||pos<1){System.out.println("Invalid");return;}
Node refNode = head,prevNode=head;
for(int i=0;i<pos-1;i++)
{
  prevNode=refNode;
  refNode=refNode.getNext();
}
prevNode.setNext(refNode.getNext());
}


void deleteAtEnd(){
if(head==null){System.out.println("Nothing to Delete..");return;}
this.length--;
Node refNode=head,prevNode=head;
while(refNode.getNext()!=null){
prevNode = refNode;
refNode = refNode.getNext();
}
System.out.println(prevNode.getData());System.out.println(refNode.getData());
prevNode.setNext(null);
}


private Node putInMiddle(Node head,int pos,int data)
{
Node newNode = new Node();
newNode.setData(data);newNode.setNext(null);
Node refHead = head;
for(int i=1;i<pos-1;i++){refHead = refHead.getNext();}
newNode.setNext(refHead.getNext());
refHead.setNext(newNode);
return head;
}


void printList(){
Node refHead = new Node();
refHead = head;
if(refHead==null){System.out.println("Nothing to Print..");return;}
while(refHead!=null)
{System.out.print(refHead.getData()+"->");refHead=refHead.getNext();}
System.out.println();
}


static private Node putNewAtEnd(Node head,int data){
Node newNode = new Node();
newNode.setData(data);newNode.setNext(null);
Node refNode = head;
System.out.println(refNode==null);
if(refNode==null){head = newNode;}
else{
while(refNode.getNext()!=null){refNode=refNode.getNext();}
refNode.setNext(newNode);
}
return head;
}


static private Node putAtBeginning(Node head,int data)
{
Node newNode = new Node();
newNode.setData(data);newNode.setNext(head);
head = newNode;
return head;
}
}

Saturday 21 June 2014

COMPLEXGENE BLOG IS BACK ..... THIS WEEK: GOOD ALGORITHMS




Hi Guys,
How has been the weekend?

This week we will come with the basic concepts of Algorithmic Complexity(the initial steps of understanding how efficient an algorithm is).

Without understanding complexity its like writing a code without knowing a syntax.

I would try to make the things as simple as i can, assuming that after you read this blog you would start thinking about good algorithms for the first time in your life :).

Ok so here we go.................

Q1) What is an algorithm?
Ans: Mostly all will be able to answer this. Its a sequence of steps that perform a particular operation, or               rather designed to achieve some particular task.
So far so good.....

Q2) What is a good algorithm then?
Ans: Ah!! Come on shan(my nickname)!! Then why we would have been in this blog? :) Right...
        So, lets get to good algorithms. Now keep your brain smart for 5 mins for you are going to get the                 algorithmic complexity in your head very soon.
        Let suppose, we need to sort 5 numbers: 12  2  11 6 54, how will you sort?
        Lets make the sorting very simple, we go through each of the element 12 then 2 then 11 then 6 and then         54, pick the smallest among them, so we pick 2, then swap 2 with the element at first position.
        So now the arrangement would be: 2 12 11 6 54.
        We go again, swap the next smallest element with first position. Now it looks like: 2 6 11 12 54 and so         on.... Well we know that after some time the array would get sorted, but what we are concerned about is: Was this a good approach though we have accomplished our task, how much would it require?
Time?? What time?? Yeah... that's what we have to get concerned about when we are being able to do our task. We have to put forth a question that whether we could have been able to do it in less time with a better strategy?  Now what will be the time required. Is it 60 mins, is it 20 secs? Is it 5 mins..
No we don't talk of time in actual hours or minutes in algorithms. We talk of time in terms of number of elements on which we are doing any processing.
So let suppose insted of 5 elements there would have been 1,00,000 elements to be sorted.. so the numbers are not fixed, time to time it is going to change but we have to perform the same task on them... 
So lets make a standard, let us call the number of elements as N.
Ah!! that's fine as is not fixed, it can be any number of elements according to the elements given  to us.
Sometime N=5, sometimes N=1,00,000.
So, now lets judge the algorithm we just discussed above for sorting:
Step 1: We are traversing all the elements and picking up the 1st smallest element out of them and then swapping it with element at first position. So to get the first smallest element we have traversed all the 'N' elements.
Step 2: For the next smallest we would need to traverse N-1 elements leaving the first position as it has already been replaced... 
Step 3: In this way to sort N elements in total we will get such a series of operation:
             ======>   N + (N-1) + (N-2) + (N-3)+..........+ 2 + 1   
             ======>   N(N+1)/2  ====> (N^2 + N)
       Ex: 1+2+3+4+5=15, similarly if N=5 then N(N+1)/2=(5*6)/2=30/2=15..... So in total we make 15 comparisons using our approach to sort 5 elements. You can manually count the number in sorting 5 elements above and you would get the same number.

Now we have understood how is the time measured... we will think of a better approach to make less comparisons for sorting N numbers.
Lets come back to the problem again..
I will try to make a better approach, you need correct me if i am wrong.. :)
Elements : 12  2  11 6 54
Step 1: We would traverse the whole array once and get the highest element from them. SO time taken is N as we are going through all the N elements.. and we get element as 54.
Step 2: We will make an array of size 54, say ARR[54] and then store each element to the corresponding index of the array, like 12 stored in ARR[12], 2 stored in ARR[2] and so on..so our array will look like:


So the array looks like this now..what to do next?
Oh yes!! One thing!! Don't think too much now as there can be elements with negative value, or an element such as zero.. After understanding you yourself try to figure out the solutions to such situations.
For now.. stick yourself to the limited Edition..  :D
So Next....
what to do?? Traverse the array from beginning and if we get a position not having 0, output that to the screen..
Starting from index 0.. we traverse... we output: 2 6 11 12 54 and voila!! That's sorted. :)
What time this time did we take?
Once we traversed the whole array through all elements to get the highest element: N
Next we traversed again through each N element to store them at corresponding index of the third array, again: N
Next, we traveled through the third array to output the elements in sorted order, again: N
So, we took 3*N time.
Okay!! So where the difference of it from the first algorithm,. Okay let suppose there are 20 elements in total.
Our first approach would take: 20(21)/2 = 210 unit time.
Our second approach would take: 3*20 = 60 unit time.
Oh!! yes!! See the difference when the number of elements is merely 20, just think of the difference when the number of elements will reach 1,00,000.

Okay!! Now i can breathe a bit as i can now assume that i have been able to make you understand what a good algorithm is and what its importance?

Today we will close here!! As now your mind is ready to think of ways to solve a problem in a better waqy, lets follow all the protocl of the algorithm in the next post and define the notion of BIG-O, BIG-OMEGA, and BIG-THETA :).. which sometimes become so confusing to various CS/IT champions.

Bye for now!! Will meet soon!! :)
Don't forget to add comments to cheer me up. :) Thank you all.