We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- C
- Functions
- Calculate the Nth term
- Discussions
Calculate the Nth term
Calculate the Nth term
Sort by
recency
|
815 Discussions
|
Please Login in order to post a comment
include
include
include
include
//Complete the following function.
int find_nth_term(int n, int a, int b, int c) { int term, t1 = a, t2 = b, t3 = c; if (n == 1) term = t1; else if (n == 2) term = t2; else if (n == 3) term = t3; else { for (int i = 4; i <= n; i++) { term = t1 + t2 + t3; t1 = t2; t2 = t3; t3 = term; } } return term; }
int main() { int n, a, b, c;
}
include
include
include
include
//Complete the following function.
int find_nth_term(int n, int a, int b, int c) { int term, t1 = a, t2 = b, t3 = c; if (n == 1) term = t1; else if (n == 2) term = t2; else if (n == 3) term = t3; else { for (int i = 4; i <= n; i++) { term = t1 + t2 + t3; t1 = t2; t2 = t3; t3 = term; } } return term; }
int main() { int n, a, b, c;
}
How Recursion works:
Here’s a step-by-step breakdown of how to calculate the 8th term of a series using recursion, showing the full expansion, substitution, and simplification process.
Expanded Breakdown for:
return find_nth_term(n - 1, a, b, c) + find_nth_term(n - 2, a, b, c) + find_nth_term(n - 3, a, b, c);
Here's the full recursive breakdown:
n = 8 , a = 1, b = 2, c = 3
n8 = (n7 + n6 + n5)
n8 = ((n6 + n5 + n4) + (n5 + n4 + n3) + (n4 + n3 + n2))
n8 = (((n5 + n4 + n3) + (n4 + n3 + n2) + (n3 + n2 + n1)) + ((n4 + n3 + n2) + (n3 + n2 + n1) + n3) + ((n3 + n2 + n1) + n3 + n2))
n8 = ((((n4 + n3 + n2) + (n3 + n2 + n1) + n3) + ((n3 + n2 + n1) + n3 + n2) + (n3 + n2 + n1)) + (((n3 + n2 + n1) + n3 + n2) + (n3 + n2 + n1) + n3) + ((n3 + n2 + n1) + n3 + n2))
n8 = (((((n3 + n2 + n1) + n3 + n2) + (n3 + n2 + n1) + n3) + ((n3 + n2 + n1) + n3 + n2) + (n3 + n2 + n1)) + (((n3 + n2 + n1) + n3 + n2) + (n3 + n2 + n1) + n3) + ((n3 + n2 + n1) + n3 + n2))
Substituting values:
n8 = (((((3 + 2 + 1) + 3 + 2) + (3 + 2 + 1) + 3) + ((3 + 2 + 1) + 3 + 2) + (3 + 2 + 1)) + (((3 + 2 + 1) + 3 + 2) + (3 + 2 + 1) + 3) + ((3 + 2 + 1) + 3 + 2))
n8 = ((((6 + 5) + 6 + 3) + (6 + 3 + 2) + 6) + ((6 + 5) + 6 + 3) + (6 + 3 + 2))
n8 = (((11 + 6 + 3) + 11 + 6) + (11 + 6 + 3) + 11)
n8 = ((20 + 11 + 6) + 20 + 11)
n8 = (37 + 20 + 11)
n8 = 68