Ask a Question

 

Start Coding Today

   

GeeksQuiz

GeeksforGeeksIDE

Data Structures

Algorithms

Interview Experiences

C Programming

C++ Programming

Java Programming

GATE CS

Books

solve 1+2X3+3X4X5+4X5X6X7…..using recursion for an input ‘n’

asked Dec 10, 2015 by passion34

4 Answers

+1 vote
 
Best answer

My logic is this :
at each iteration two new numbers are coming, so we will multiply those number and save in cur, then rest of the part we can get from last term by dividing last term by its first number, for example -
for calculating   4*5*6*7        at this iteration(4)  -> new terms are  6 and 7 (2*4-1,2*4-2)
and last term is 3*4*5 ,  now if we divide it by its first number(3)  we will get  4*5
so we can multiply this number to cur to get our exact value.

code for same is given below - 

#include <stdio.h>
//  1  2*3  3*4*5  4*5*6*7  5*6*7*8*9  6*7*8*9*10*11  7*8*9*10*11*12*13
int N;

int solve(int t, int n)
{
    if(n == N + 1)
        return 0;
    int cur = (2*n - 2)*(2*n - 1); 
    cur = cur * (t / (n - 1));
    return cur + solve(cur, n + 1);
}

int main()
{
    scanf("%d", &N);
    printf("%d", 1 + solve(1, 2));
}

calling function  argument are 1 and 2 i.e.   I am starting calculation from 2nd term with my previous sum as 1.

answered Dec 10, 2015 by utkarsh111
selected Dec 10, 2015 by passion34
0 votes

making use of global variables :
 

// recursion.cpp : Defines the entry point for the console application.
//

#include<iostream>
using namespace std;

int size = 0;
int sum = 0;

void calculate(int n)
{
    if(n > size)
    {
        return;
    }
    else
    {
        int temp = 1;
        for(int i = 0; i < n; i++ )
        {
            temp = temp * (n+i); 
        }
        sum = sum + temp;
        calculate(n+1);
    }
}


int main()
{
    cout << "Enter the Number..!!" << endl;
    cin >> size;

    calculate(1);
    cout << "sum = " << sum << endl;

    return 0;
}

answered May 11, 2016 by anonymous
0 votes
def seq_gen(n):
        if n == 2:
                return 6
        else:
                s = (6 + (n-2)*4)*seq_gen(n-1)
                return s

def sum_seq(n):
        if n == 1:
                return n
        else:
                su = 1
                for i in range(2,n+1):
                        su += seq_gen(i)
                return su

print sum_seq(3)
print sum_seq(4)

This logic works with respect to generating sequence of terms - ratio (i+1)th term to ith term. This sequence is arithmetic progression sequence will be 6,10,14,18 ...... (n-1 terms). So, for example, getting 3rd term = 6*10 hence, add such terms for n, for example, n= 3, sum of 1,6,60 = 67 and same for any n (>1).

answered May 12, 2016 by Krishnasagar Subheda
0 votes

int solve(int terms) {    
    if (terms == 1)
        return 1;
    
    int product = 1;
    int count = 0;
    int val = terms;
    
    while(count < terms) {
        product  = product*(val + count);
        count++;
    }
    
    return product + solve(terms - 1);
}

answered May 12, 2016 by Bharat Singh

Please log in or register to answer this question.

...