歐拉計劃----https://projecteuler.net/spa
偶斐波那契數
斐波那契數列中的每一項都是前兩項的和。由1和2開始生成的斐波那契數列前10項爲:.net
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, … 考慮該斐波那契數列中不超過四百萬的項,求其中爲偶數的項之和。code
public class Problem2 { static long cal(long num) { long s=0; long theBeforeNum=1; long theAfterNum=2; long temp=0; while (theAfterNum<num) { if(theAfterNum%2==0) { s+=theAfterNum; } temp=theAfterNum; theAfterNum=theBeforeNum+theAfterNum; theBeforeNum=temp; } return s; } public static void main(String[] args) { long start = System.currentTimeMillis(); System.out.println(cal(4000000)); long end = System.currentTimeMillis(); System.out.println("runtime:" + (end - start)); } }