/* * bhatt.umang7@gmail.com */ /* * 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. */ public class Problem2 { public static void main(String args[]) { long ans = 0 ; long sum = 0 ; long max = 4000000; long prev_prev = 0 ; long prev = 1 ; while (sum< max) { sum = prev_prev + prev ; prev_prev = prev ; prev = sum ; if (sum%2 == 0) { ans += sum ; } } System.out.println("Ans is "+ ans ); // ans is } }
Thursday, January 19, 2012
sum of even Fibonacci numbers less than 4 million
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment