Question:
How to calculate the sum of terms in Recamans sequence using C++

Summary

Let’s consider a function is defined named F as follows F(n)= (1) +(2*3) + (4*5*6) ... upto n terms.

We will define the class that have two member functions ‘f’, and ‘sequence’ 


Solutions

//User function Template for C++


//User function Template for C++


class Solution{

public:

    int mod=1e9+7;


    long long f(int term, int start, int n){

        if(term > n){

            return 0;

        }

        

        long long int ans=1;

        for(int i=0;i<term;i++){

            ans=ans*start;

            ans=ans%mod;

            start++;

        }

        

        ans=ans+f(term+1, start, n);

        ans=ans%mod;

        

        return ans;

    }


    long long sequence(int n){

        // code here

        long long ans= f(1, 1, n);

        

        return ans;

    }

};


Explanation

mod variable:

  • This variable is set to 

1

0

9

+

7

10 

9

  •  +7, which is a common choice for modulo operations in competitive programming to prevent integer overflow.


f function:

  • This function calculates the sum of terms in a sequence.

  • term: Current term of the sequence.

  • start: Value of the first term.


sequence function:

  • This function is the entry point for calculating the sequence.

  • n: Total number of terms in the sequence.


Suggested blogs:

>Find the number of pairs of elements whose sum is equal to K using C++

>How to check whether string is palindrome or not using C++

>How to determine the smallest possible size of a vertex cover using C++

>Build an Electron application from scratch

>Building Web API using ASP.NET (C#)

>Built Web API using ASP.NET (C#)


Nisha Patel

Nisha Patel

Submit
0 Answers