Calculate the Nth term

Sort by

recency

|

815 Discussions

|

  • + 1 comment
    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    #include <stdlib.h>
    //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;
      
        scanf("%d %d %d %d", &n, &a, &b, &c);
        int ans = find_nth_term(n, a, b, c);
     
        printf("%d", ans); 
        return 0;
    }
    
  • + 0 comments

    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;

    scanf("%d %d %d %d", &n, &a, &b, &c);
    int ans = find_nth_term(n, a, b, c);
    
    printf("%d", ans); 
    return 0;
    

    }

  • + 0 comments

    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;

    scanf("%d %d %d %d", &n, &a, &b, &c);
    int ans = find_nth_term(n, a, b, c);
    
    printf("%d", ans); 
    return 0;
    

    }

  • + 0 comments

    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

  • + 0 comments
    if(n == 1) return a;
    else if ( n == 2) return b;
    else if ( n == 3) return c;  
    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);