Wednesday, June 13, 2012

FIND THE DAY ON A PARTICULAR DATE


Most of the times,  you will be finding the day in 19th ,20th or 21st century.

So, remember, 19th century started with a sunday(january 1, 1800 was a sunday), 20th century with first working day i.e. monday(january 1, 1900 was a monday).

so if the concerned date is in 19th century. take 1 jan, 1800(sunday) as a reference. else if it is after 19th century, take 1 jan, 1900(monday)as a reference.

the scale i used is 0-6(i.e. if friday is 0 then saturday will be 1, sunday will be 2 and so on till thursday will be 6).

Main Method():

lets discuss this method with a date say (3 april 1990)

Take the reference as described above.

1. now you know first day of the century. so now you can find the first day of any year lies within that century.

in our example. 1 january 1990 can be calculated as:

* remember the word LID or DIL(hindi for Heart) whatever suites you.

L: leap years between the year concerned(not included) and reference year(included).
I: I symbolises 1
D: difference between the year concerned and reference year.

add these and divide by 7. the remainder is the day of the year with respect to the reference year.

(here, there are 21 leap years between 1990 and 1900   +   1   +   the difference is 90).
it comes out to be 112
divide this number by 7 that is 112/7 = 0 is the remainder.



So, 1 january 1990 is monday(1 january 1900)+0(remainder)= monday.

2. now to calculate first day of the concerned month of the concerned year.
remember a number series, i.e.

358-0-358-1-368

as 1 January of the year concerned is known, the day at any date of the January can be calculated easily(no need to tell). so , the series above starts with the month February.

one more thing, the number in the series represents the following number from February to December respectively.
3 - 3
5 - 5
8 - 8
0- 10
3- 13
5- 15
8- 18
1- 21
3- 23
6- 26
8- 28
(because, it is easy to learn single digits than double digits, obviously).

now to calculate concerned month
(deduct two from the month code above(if its not a leap year) else 1).
and divide the outcome by 7 and remainder is the first day of that month. with respect to 1 January of that year.
never deduct anything from February code as leap year does not affect first date of February (1 Feb. comes before 29 February).


in our example, April is the month so deduct 2 from 8(as 1990 is not a leap year) i.e. 6 and divide the 6 by 7  which gives the remainder 6.

hence first day of April is [Monday(1 January 1990) + 6(remainder)] = Sunday

which gives third April as Tuesday.

so two things to remeber are:

1. LID
2. 358-0358-1-368
--------------------------------------------------------------------------------------------------

lets discuss one more problem:

8 November 1992(leap year).
applying  LID to 1992:
21 +1 + 92 =114/7 = (16+2) = 2 is the remainder

so the first day of 1992 is Moday(1 January 1900) + 2 = Wednesday


now to find the first day of November of the year 1992.

apply the number series.
November's code is 26
so deduct 1 from 26 (as 1992 is a leap year) and divide the outcome by 7
so 25/7 = 4 is the remainder.
so first day of November is Wednesday + 4 = Sunday
so , 8th of November, 1992 is also Sunday.
---------------------------------------------------------------------------------------------------


believe me, it just looks like a long method, actually if you practice it few times, it just comes down to two or three things.


Thank you,

hope it helps,

leave your feedback in the comment. I would appreciate it.

Sunday, June 10, 2012

how to calculate log of a number without using log table

Sometimes, it is necessary to find the log(logarithm) of a number when there is no log table around.
so, the only thing you got is your mind.

below is a method which can be used to calculate the logarithm of a number if there is no log table to use.
this is also helpful if the number is too large to be calculated by a calculator. (the mind does n)

first of all, remember the following basic log values (its no harm to remember these even if you have log table).

log 2 = 0.301

log 3 = 0.477

log 5 = 0.699

log 7 = 0.845


these four values can be applied to calculate the log of any number (size does not matter).


and a formula:

log(a+b)=log a + ( b / (2.42*a) )

main method();

for example consider a small value:

log 737=?

=>log (737) = log (7.37 * 10^2)

=>log (7.37 ) + log (10^2)     [ log mn = log m + log n]

=>log (7.37) +2                     [ log 10^n = n]

=>log(7 + 0.37) + 2

now apply the formula:

=>log(7)+ 0.37/2.42*7 + 2

it comes out to be 2.867

which the log value for 737.

now this technique can be applied to any number.

Thank you,

hope it helps.

Saturday, June 9, 2012

how to learn complexities of sorting algorithms

The sorting algorithms we usually come across are:

Bubble , Insertion, Heap, Merge, Selection and Quick Sorting and it is very difficult to learn the complexities of all these algorithms. It becomes a more tedious task when it comes to three cases of complexities(Worst, Average, Best).

Below is a method to memorize all the complexities(including average, best and worst cases) of these six algorithms.

First letter of every sorting stands for that sorting. For ex.- B stands for Bubble sort, I stands for Insertion sort and so on....
there are only types cases of complexities:
  1. n
  2. n2(n square)
  3. nlogn
(dont worry about the complexity "n", it is not used frequently)

the trick to remember is as follows:

prepare a table in which first column contains all the algorithms names(in any order)
and first row contains all three cases(worst, average and best) as shown in figure in the specified order(i.e. increasing from worst to best).


Now, the words you have to remember are


  • BIn

B means bubble sort
I means insertion sort

both Bubble and insertion sort have same set of complexities but their best case is different than other. so remember "BIn": short for "Best Is n".


  • HtMl

H: Heap
M: Merge
t: just to remeber the whole word. it is of no use otherwise.
l means complexity "nlogn".

both Heap and Merge have same set of complexities and also all of them are nlong(l).


  • S stands for selection sort and "square". so all three of selection sort are n2.

  • Qsl(Queen of  scott-land): first column of quick sort is n2(as "s" in scott) and rest are nlong (as l in land).
Note: nlogn is used whenever it is specified otherwise use n2 as default
like: in BIn, use n2 in average and worst cases.
as I said earlier that complexity "n" is not much of use as it is used only twice that too can easily be memorize as "best is n".


Practice this two or three times and you will remember it as good as anything.







Thank you,

Please feel free to leave any comment or suggestion.