`

projecteuler.net第二题

阅读更多
http://projecteuler.net/index.php?section=problems&id=2

projecteuler.net第二题
Find the sum of all the even-valued terms in the Fibonacci sequence which do not exceed four million.
求Fibonacci数列中小于4百万的偶数之和.
分享到:
评论
1 楼 lampeter123 2009-07-08  
第二题答案:4613732
public class Problem_2 {
       public static long F(long n) {
                if (n==1||n==2) {
                        return 1;
                } else {
                        return F(n-2) + F(n-1);
                }
        }
 
  public static void main(String args[]) {
          long sum = 0, n;
         
          for (long i =2; F(i)<4000000; i++) {
                  n = F(i);
                  if (n%2==0) {
                          sum += n;
                  }
          }
         
          System.out.println(sum);
  }
}

相关推荐

Global site tag (gtag.js) - Google Analytics