October 31, 2010

My Student Logic


Lenin Loganathan

I am very happy to introduce one of my student to this world. His name is Lenin Loganathan. He is studying MCA in our college. Currently he is in first semester. I am teaching Web Designing subject for his class.

Now we are solving one problem in Java Script. The problem is to generate lucky number based on user's date of birth. There are two condition for this problem.
  1. The lucky number should be sum of day, month and year. 
  2. The lucky number should be single digit. 
Let us consider August 29, 1984 as date of birth.

Day = 29
Month = 8
Year = 1984

According to first condition the lucky number should be sum of day, month and year.

Lucky Number = Day + Month + Year

Lucky Number = 29 + 8 + 1984
Lucky Number = 2021

As per second condition the lucky number should be single digit. So the sum of digits will be the one of the solution.

Lucky  Number = 2 + 0 + 2 + 1
Lucky  Number = 5

Most of the people prefer the following logic to find the sum of digits.

Number = 2021
Sum = 0
while(Number != 0)
{
     Sum =  Sum + (Number % 10)
     Number = Number / 10
}
document.write("Lucky Number is: " + Sum);


For this problem, Lenin find one alternate logic to get a sum of digits.


Sum of day, month and year is always four digit number only. So Lenin says that divide any four digit number by nine, the remainder of this division operation is always sum of digits only.


Number = 2021;
if (Number %  9  == 0)
{
      document.write("Lucky Number: " + 9);
}
else
{
      document.write("Lucky Number: " + Number %  9);
}


I always encourage my students to come with an alternate logic. Initially Lenin also used the first logic only. Later I asked him to come with an alternate logic. At the end he came with this logic. Moreover the interesting fact is Lenin find this logic within two hours. 


Note: August 29, 1984 is one of my friend (Rajesh.S) birthday. In my batch, he is known for logic. I used to call him as Logic Rajesh.