Saturday, January 25, 2014

Project Euler Problem 2

Problem 2: Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

                                                   1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

Solution:
In general the n’th number in the Fibonacci sequence is defined as Fn = Fn-1 + Fn-2.
So here is the brute force approach:
This approach is very easy. Just compute the next Fibonacci number and check if its even or not. If it is even, then add it to the sum otherwise move on.

 #include <stdio.h>  
 int main(void)  
 {  
      int sum=2,first=1, second=2, third=3;  
      while(third<4000000)  
      {  
           if(third%2 == 0)  
         {  
                sum+=third;  
           }  
         first = second;  
           second = third;  
           third = first + second;  
      }  
      printf("%d",sum);  
      return 0;       
 }  

Now lets try to get an efficient solution. If we look at the Fibonacci sequence:
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89
we can notice the pattern that every third number is even starting from third term. So nth term can be expressed as:
F(n) = F(n-1) + F(n-2)
We want to express this term in terms of 3 and 6. So,
F(n) = F(n-2)+F(n-3)+F(n-2)=2 F(n-2) + F(n-3)
F(n) = 2(F(n-3)+F(n-4))+F(n-3))=3 F(n-3) + 2 F(n-4)
F(n) = 3 F(n-3) + F(n-4) + F(n-5) + F(n-6)
F(n) = 4 F(n-3) + F(n-6)

I leave the code as an exercise to the reader. Thanks for reading the post. If you liked it then do comment.

Project Euler Problem 1

Hi Friends,

I started solving Project Euler problems recently and found them to be really interesting. So I though of discussing my approach and solutions with the world. I will code my answers in C. So here we start.

Problem 1: If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.

The problem is pretty easy and straight forward. Just check all the numbers from 3 to 999 if they are divisible by 3 or 5 and add them if they are.
Here is my code:
1:  #include <stdio.h>  
2:  int main(void)  
3:  {  
4:       int sum=0,i;  
5:       for(i=3;i<1000;i++){  
6:            if(i%3==0 || i%5==0)  
7:                 sum+=i;  
8:       }  
9:       printf("%d",sum);  
10:       return 0;       
11:  }  
Do comment if you have any alternate solution.

Sunday, March 18, 2012

My Experience: JCA at IIT Delhi


Hi Friends, This is my first post regarding my experience of JCA at IIT Delhi. When I joined JCA (M.Tech in Joint Computer Applications), I was quiet confused whether I should join this program or not. But as days passed by, I came to know that JCA was the best choice I have ever made.

JCA is one of the oldest programs in IITD (Started before M.Tech in Computer Science). It was initially started by Mathematics department but as computer science department was formed, it was then jointly run by Computer Science Department, Mathematics Department and Electrical Department. JCA gives us a lot of flexibility while doing course selection. We can live life according to us. We can make our life easy/difficult/hard.

Pros I felt that JCA has over MCS and EET (M.Tech in Computer Technology)
  • In JCA, unlike MCS we don’t have too much lab pressure. This gives us opportunity to focus on other subjects like Algorithms, Networks, etc. The early focusing surely helps in selecting the minor project in 2nd semester.
  •             In 2nd Semester, we don’t have compulsory minor project as in MCS. So those who want to focus on studies rather than project have good opportunities. (Many MCS guys didn’t wanted to do project. They are unhappy for this.)
  •            We can do project under any of Computer, Electrical, or Mathematics department. (Flexibility)
  •           We are not given too much of TA work in JCA. (MCS students spend around 8+ hours every week for unnecessary TA work). So therefore we get more time for focusing on studies. (And write blog too!!)
  •            Students from B.Tech background always have an edge over students from M.Sc background. So you get to score good grades in common subjects.
Cons of JCA:
  • JCA has 2 compulsory Mathematics Subjects (Discrete Mathematics and Numerical Optimization). They are quiet easy but may be you won’t like them.
  • We don’t have good lab for night use. (We use our own laptop instead). This problem is only for first year though.
  • JCA is not known outside IIT Delhi, but that doesn't make much difference as all students get placed in campus itself.

Placements:
  • As JCA has mathematics department as its parent department, the students of JCA gets opportunity to sit in Banking and finance companies which is extremely good if you don’t like coding.
  • We know many of our seniors from computer science background placed in Amazon/ Google/Microsoft/Adobe/AMD/Nokia Siemens etc.

Conclusion:
I am happy and satisfied that I joined JCA. JCA students get equal opportunities as MCS students.

Sunday, October 17, 2010

Microsoft and Facebook Partner On Social Search Feature

Microsoft Corp. and Facebook today expanded their partnership by unveiling a feature that lets users of Microsoft's Bing search engine find websites preferred by their Facebook friends.
The new feature allows Bing users to see pictures of their Facebook friends next to certain Websites those people said they "liked" on their Facebook pages, the companies said today.


"This new signal will allow us to do a better and more comprehensive job predicting what resources and content are most relevant to you because, in addition to all the other signals we use, other people you trust have found them interesting," wrote Satya Nadella, senior vice President, online services division at Microsoft, in his blog post. "It means better, more personal search experiences and better tools and input to help you make decisions."
The companies hope to boost usership by developing features that cut through Internet clutter.
Microsoft, the world's largest software maker, seeks to close the gap between Bing and search-engine leader Google Inc. Last month, Google had a 66% market share of U.S. searches, compared to 11% for Microsoft sites.

Meanwhile, Facebook is adding features to try to extend its lead over MySpace as the biggest player in the rapidly-growing social-networking space. Facebook in August was the fourth-most popular U.S. web property, behind only sites collectively owned by Yahoo! Inc., Google and Microsoft, ComScore said last month.