Fibonacci 斐波那契数列第n个数的求解,也可以用递归和非递归的形式实现,具体如下,dart语言实现。
int fibonacci(int n) {
if (n <= 0) throw StateError('n cannot be <= 0!');
return n > 2 ? fibonacci(n - 1) + fibonacci(n - 2) : 1;
} int fibonacciNonrecursive(int n) {
if (n <= 0) throw StateError('n cannot be <= 0!');
if (n < 3) return 1;
int a = 1, b = 1;
while (n-- > 2) {
b += a;
a = b - a;
}
return b;
}