有一分数序列:2/1, 3/2, 5/3, 8/5, 13/8, 21/13....求出这个数列的第M到N项之和(M>2,N>2,N>M)
package bianchengti; /* * 有一分数序列:2/1, 3/2, 5/3, 8/5, 13/8, 21/13.... * 求出这个数列的第M到N项之和(M>2,N>2,N>M) */ public class getSumByMN { //返回项:1, 2, 3, 5, 8, 13, 21.... public static int getItem(int n) { if(n<0) return 0; int []array = {0,1,2}; if(n<3) return array[n]; return getItem(n-1)+getItem(n-2); } //返回项:2/1, 3/2, 5/3, 8/5, 13/8, 21/13.... public static double getNumber(int n) { return (double)getItem(n+1) / getItem(n); } //返回第M到N项之和 public static double getSum(int m,int n) { double sum = 0.0; for(int i = m; i <= n; i++){ sum += getNumber(i); } return sum; } public static void main(String[] args) { // TODO Auto-generated method stub System.out.println(getSum(3, 4)); //3.2666666666666666 } }
追梦的脚步,永不停息