Program to print the sum of the given nth term


The problem statement includes printing the sum of the series whose Nth term is given.

The value of N will be given in the input. We need to find the sum of the sequence up to N where the Nth term of the sequence is given by:

$$\mathrm{N^{2}−(N−1)^{2}}$$

Let’s understand the problem with the below examples:

Input

N=5

Output

25

Explanation − The value of N given is 5.The first 5 terms of the sequence are:

$\mathrm{N=1,1^{2}−(1−1)^{2}=1}$

$\mathrm{N=2,2^{2}−(2−1)^{2}=3}$

$\mathrm{N=3,3^{2}−(3−1)^{2}=5}$

$\mathrm{N=4,4^{2}−(4−1)^{2}=7}$

$\mathrm{N=5,5^{2}−(5−1)^{2}=9}$

The sum of the terms of the sequence until 5th term is 1+3+5+7+9=25. Hence, the output is 25.

Input

N=8

Output

64

Explanation − Calculate the sum of the sequence up to 8th term by using the formula for Nth term of the sequence,

$\mathrm{N=1,1^{2}−(1−1)^{2}=1}$

$\mathrm{N=2,2^{2}−(2−1)^{2}=3}$

$\mathrm{N=3,3^{2}−(3−1)^{2}=5}$

$\mathrm{N=4,4^{2}−(4−1)^{2}=7}$

$\mathrm{N=5,5^{2}−(5−1)^{2}=9}$

$\mathrm{N=6,6^{2}−(6−1)^{2}=11}$

$\mathrm{N=7,7^{2}−(7−1)^{2}=13}$

$\mathrm{N=8,8^{2}−(8−1)^{2}=15}$

The sum will be 1+3+5+7+9+11+13+15=64, which is the required output.

Let’s see the different approaches to solve the problem in C++.

Approach 1

The naive approach to the problem can be just calculating every term of the sequence until N and keep adding them to get the sum of the sequence up to N where Nth term of the sequence is $\mathrm{N^{2}−(N−1)^{2}}$

The steps to follow to implement the approach in C++ to get the sum of the sequence until N:

  • We will simply create a function to calculate the sum of the sequence until its Nth term where each term of the sequence is given by $\mathrm{N^{2}−(N−1)^{2}}$

  • Initialise a variable to store the sum of the sequence of N terms. The variable must be of long long data type just to make sure the values of sum for larger values of N.

  • Iterate in a for loop from i=0 to i<=N to calculate the sum of the sequence for N terms.

  • For every iteration, we will add the sum of the ith term in the variable using the formula $\mathrm{N^{2}−(N−1)^{2}}$ as it represents the Nth term of the sequence.

  • Return the variable after iterating until N which will be the sum of the sequence of first N terms, where N will be the user input.

Example

//C++ code to find the sum of the sequence of first N terms where
//Nth term is given by N^2-(N-1)^2

#include <bits/stdc++.h>

using namespace std;

//function to calculate the sum of the sequence until N
long long sum(int N){
    
    long long a=0; //to store the sum of the sequence
    
    //iterating to calculate every term of the sequence from 1 to N
    for(int i=1;i<=N;i++){
        
        //using the formula N^2-(N-1)^2
        a = a + (i * i - (i-1)*(i-1)); //adding each term of the sequence until N
    }
    
    return a;
}

int main()
{
    int N;
    N=8; //input
    //calling the function
    cout<<"The sum of the sequence up to "<<N<<" terms is: "<<sum(N)<<endl;
    
    N=54;
    cout<<"The sum of the sequence up to "<<N<<" terms is: "<<sum(N)<<endl;

    return 0;
}

Output

The sum of the sequence up to 8 terms is: 64
The sum of the sequence up to 54 terms is: 2916

Time complexity− O(N) , because we iterate in a for loop until N to calculate every term of the sequence and add it.

Space complexity − O(1) , because no extra space is taken to find the sum.

Approach−2 (Efficient approach)

Despite calculating every term of the sequence and adding them simultaneously using the formula for Nth term of the sequence, we can use the concept of sum of N terms of an Arithmetic progression (A.P.) as the sequence is forming an A.P.

If we calculate first few numbers of the sequence using the formula for Nth term of the sequence i.e. $\mathrm{N^{2}−(N−1)^{2}}$ and observe the pattern, we get

The first number of the sequence is 1.

The second number of the sequence will be 3.

Similarly, $\mathrm{N=3,3^{2}−(3−1)^{2}=5}$

$\mathrm{N=4,4^{2}−(4−1)^{2}=7}$

$\mathrm{N=5,5^{2}−(5−1)^{2}=9}$

The first few numbers of the sequence calculated using the formula are 1, 3, 5, 7, 9, 11, 13…….

We can see that the sequence is just an A.P. with its first term as 1 and the common difference as 2.

The sum of first N terms of an A.P. is given by,

$\mathrm{S_{N}=\frac{N}{2}(2*a+(N−1)d)}$, where a=first term of the AP and d= common difference of the AP.

To calculate the sum of the sequence until N terms, we will use the above formula. Replacing the values, we get

$\mathrm{Sum\:of\:the\:sequence=\frac{N}{2}(2*1+(N−1)2)\:as\:(a=1\:and\:d=2)}$

$$\mathrm{=\frac{2*N}{2}(1+N−1)=N^{2}}$$

The sum of the sequence up to its Nth term where Nth term is given by $\mathrm{N^{2}−(N−1)^{2}}$ is equal to the square of N itself, where N is the number of terms.

The steps to follow to implement the approach in C++:

  • We will make a function to get the sum of the sequence up to Nth term whose Nth term is $\mathrm{N^{2}−(N−1)^{2}}$

  • Initialise a variable to store the sum of the sequence and store the value of square of N in it as it gives the sum of the sequence of first N terms.

  • Return the variable which will be the required output.

Example

//C++ code to find the sum of the sequence of first N terms where
//Nth term is given by N^2-(N-1)^2

#include <bits/stdc++.h>

using namespace std;

//function to calculate the sum of the sequence until N terms
long long sum(int N){
    
    long long a=0; //to store the sum of the sequence
    
    a = N*N; //the formula to give the sum of sequence up to N terms
    
    return a;
}

int main()
{
    int N;
    N=75; //input
    
    //calling the function
    cout<<"The sum of the sequence up to "<<N<<" terms is: "<<sum(N)<<endl;
    
    N=1000;
    cout<<"The sum of the sequence up to "<<N<<" terms is: "<<sum(N)<<endl;

    return 0;
}

Output

The sum of the sequence up to 75 terms is: 5625
The sum of the sequence up to 1000 terms is: 1000000

Time complexity− O(1) , constant time is taken to calculate the sum of sequence.

Space complexity − O(1) , because no extra space is taken.

Conclusion

The problem to find the sum of the sequence whose Nth term is $\mathrm{N^{2}−(N−1)^{2}}$ was discussed in the article. We discussed the naive approach to solve the problem to an efficient solution to find the sum of the sequence up to N terms in C++ within constant time and space.

I hope you understand the problem and the approach towards the problem after reading this article.

Updated on: 28-Aug-2023

120 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements