50,530
社区成员
发帖
与我相关
我的任务
分享
//1 + 2 + 3 +... + n
private static int sum1(int n){
return (1 + n) * n >> 1;
}
// 1+1+2+3+5+8+13+……+n
private static int sum2(int n){
double an = (Math.pow((1 + Math.sqrt(5d)) / 2, n) - Math.pow((1 - Math.sqrt(5d)) / 2, n)) / Math.sqrt(5d);
double preAn = (Math.pow((1 + Math.sqrt(5d)) / 2, n - 1) - Math.pow((1 - Math.sqrt(5d)) / 2, n - 1)) / Math.sqrt(5d);
return (int) (an * 2 + preAn -1);
}