Saturday, January 14, 2012

Problem 2


Problem Statement :Problem 2


Algorithm:
Generating the Fibonacci series can be read here
    Solution:
    A linear time algorithm can is written here:

        public class problem2 {
         public static void main(String args[]) {
          int a = 1, b = 2;
          int sum = 2;
          while (true) {
           int c = a + b;
           a = b;
           b = c;
           if (c > 4000000)
            break;
        
           if (c % 2 == 0)
            sum = sum + c;
          }
          System.out.println(sum);
         }
        }
        

      No comments:

      Post a Comment