Wednesday, July 10, 2013

Priorities (The Mayonnaise Jar)

Just read this, again, from the Internet. Nice story to share.

When things in your life seem almost too much to handle,
When 24 hours in a day is not enough,
Remember the mayonnaise jar and 2 cups of coffee.

A professor stood before his philosophy class
And had some items in front of him.
When the class began, wordlessly,
He picked up a very large and empty mayonnaise jar
and proceeded to fill it with golf balls.

He then asked the students, if the jar was full.
They agreed that it was.

The professor then picked up a box of pebbles and poured
them into the jar. He shook the jar lightly.
The pebbles rolled into the open Areas between the golf balls.

He then asked the students again if the jar was full. They agreed it was.

The professor next picked up a box of sand and poured it into the jar.
Of course, the sand filled up everything else.
He asked once more if the jar was full. The students responded with a unanimous 'yes.'

The professor then produced two cups of coffee from under the table and poured the entire contents into the jar, effectively
filling the empty space between the sand. The students laughed.

'Now,' said the professor, as the laughter subsided,
'I want you to recognize that this jar represents your life.
The golf balls are the important things - family,
children, health, Friends, and Favorite passions.
Things that if everything else was lost and only they remained, Your life would still be full.

The pebbles are the other things that matter like your job, house, and car.

The sand is everything else --The small stuff.

'If you put the sand into the jar first,' He continued,
'there is no room for the pebbles or the golf balls.
The same goes for life.

If you spend all your time and energy on the small stuff,
You will never have room for the things that are important to you.


Pay attention to the things that are critical to your happiness.
Play with your children.
Take time to get medical checkups.
Take your partner out to dinner.

There will always be time to clean the house and fix the disposal.

'Take care of the golf balls first --
The things that really matter.
Set your priorities. The rest is just sand.'

One of the students raised her hand and inquired what the coffee represented.

The professor smiled.
'I'm glad you asked'.

It just goes to show you that no matter how full your life may seem,
there's always room for a couple of cups of coffee with a friend.'

Thursday, July 4, 2013

Greatest Common Divisor (Eucledian)

Greatest Common Divisor (GCD for short) is the largest positive integer that divides two numbers. In other words, the greatest common divisor of a and b is, as its name suggests, the largest positive integer d such that d | a and d | b. In this post, we will see how we can calculate GCD efficiently using an algorithm that is known as Eucledian Algorithm.

The key thing in this algorithm is division with remainder, which is simply the method of "long division" that you learned in elementary school. Thus if a and b are positive integers and if you attempt to divide a by b, you will get a quotient q and a reminder r.

Below is the theorem used for this algorithm:
Let a and b be positive integers with a >= b. The following algorithm computes gcd(a,b) in a finite number of steps.
(1) Let r0 = a and r1 = b
(2) Set i = 1
(3) Divide ri-1 by ri to get a quotient qi and remainder ri+1, ri-1 = ri.qi + ri+1 with 0 <= ri+1 <= ri
(4) If the remainder ri+1 = 0, then ri=gcd(a,b) and the algorithm terminates.
(5) Otherwise, ri+1 > 0, so set i = i + 1 and go to Step 3.
Below is the sample source code on how it is implemented using the C programming language

int gcd ( int number1 , int number2 )
    if ( number2 == 0 )
        return number1 ;
        return gcd ( number2 , number1 % number2 ) ;

Hope it helps anyone who needs it. Happy Programming :)