The Fibonacci sequence is a well-known sequence of integers that follows a patte
ID: 666636 • Letter: T
Question
The Fibonacci sequence is a well-known sequence of integers that follows a pattern that can be seen to occur in many different areas of nature, and has even been heavily applied to art. The sequence looks like
0,1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ....
That is, the sequence starts with 0 followed by 1 , and then every number to follow is the sum of the previous
two numbers. Create a pseudocode function (with header) that takes an integer k as input, and returns the
kth term of the sequence. Furthermore, the function must be recursive.
The Fibonacci sequence is a well-known sequence of integers that follows a pattern that can be seen to occur in many different areas of nature, and has even been heavily applied to art. The sequence looks like 0,1, 1, 2, 3, 5, 8, 13, 21, 34, 55, .... That is, the sequence starts with 0 followed by 1 , and then every number to follow is the sum of the previous two numbers. Create a pseudocode function (with header) that takes an integer k as input, and returns the kth term of the sequence. Furthermore, the function must be recursive.Explanation / Answer
int fib(int n)
{
int p = 0;
int c = 1;
int r = 0; // The result is initialized to 0 (undefined).
for (int i = 0; i < n; i++)
{
r = p + c; // Produce next number in the sequence.
p = c; // Save previous number.
c = r; // Save current number.
}
return r;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.