nacci(List実行結果:sequence) { int type = sequence.length; return calc(int n) { if (sequence.length < n + 1) { int value = 0; for (int i = 1; i <= type; i++) { value += calc(n - i); } sequence.add(value); } return sequence[n]; }; } void main() { var fibo = nacci([0, 1]); var tri = nacci([0, 0, 1]); var tetra = nacci([0, 0, 0, 1]); var lucas = nacci([2, 1]); for (int i = 0; i <= 10; i++) { print("$i : ${fibo(i)}, ${tri(i)}, ${tetra(i)}, ${lucas(i)}"); } }
0 : 0, 0, 0, 2 1 : 1, 0, 0, 1 2 : 1, 1, 0, 3 3 : 2, 1, 1, 4 4 : 3, 2, 1, 7 5 : 5, 4, 2, 11 6 : 8, 7, 4, 18 7 : 13, 13, 8, 29 8 : 21, 24, 15, 47 9 : 34, 44, 29, 76 10 : 55, 81, 56, 123
参考:wikipedia:フィボナッチ数
0 件のコメント:
コメントを投稿