If you have completed the Tribonacci sequence kata, you would know by now that mister Fibonacci has at least a bigger brother. If not, give it a quick look to get how things work.
Well, time to expand the family a little more: think of a Quadribonacci starting with a signature of 4 elements and each following element is the sum of the 4 previous, a Pentabonacci (well Cinquebonacci would probably sound a bit more italian, but it would also sound really awful) with a signature of 5 elements and each following element is the sum of the 5 previous, and so on.
Well, guess what? You have to build a Xbonacci function that takes a signature of X elements - and remember each next element is the sum of the last X elements - and returns the first n elements of the so seeded sequence.
代码:
import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class Xbonacci { public static double[] xbonacci(double[] signature, int n) { List<Double> list=new ArrayList<>(); List<Double> sum=new ArrayList<>(); int length=signature.length; Double s=0.0; for(double db:signature){ list.add(db); s=s+db; } list.add(s); while(list.size()<n){ sum=list.subList(list.size()-length,list.size()); getListData(length,list,sum); } double[] arr=new double[list.size()]; for(int k=0;k<list.size();k++){ arr[k]=list.get(k); } return arr; } public static List<Double> getListData(int length,List<Double> list,List<Double> sum){ Double ss=0.0; for(int i=0;i<sum.size();i++){ ss=ss+sum.get(i); } list.add(ss); return list; } public static void main(String[] args) { double[] d={1,0,0,0,0,0,1}; System.out.println(Arrays.toString(xbonacci(d,10))); } }