Dãy Fibonacci: F₀=0, F₁=1, Fₙ=Fₙ₋₁+Fₙ₋₂. Dãy: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34...
Bài toán: Tính tổng bình phương các số Fibonacci từ F₀ đến Fₙ. Ví dụ n=4: 0²+1²+1²+2²+3² = 0+1+1+4+9 = 15.
Ví dụ minh họa
#include <iostream>
using namespace std;
int main() {
int n = 4;
int first = 0, second = 1, next;
int sum = 0;
for (int c = 0; c <= n; c++) {
if (c <= 1)
next = c;
else {
next = first + second;
first = second;
second = next;
}
sum += next * next;
}
cout << sum;
return 0;
}
Kết quả
15