Infinite Sum(https://www.codechef.com/CBST2021/problems/INFTSM)

Practice

Author: noob_tech
Tester: noob_tech
Editorialist: noob_tech

DIFFICULTY:

CAKEWALK, SIMPLE, EASY.

PREREQUISITES:

Math .

PROBLEM:

CodeMaster is given an integer N. CodeMaster has to find the sum of series upto N terms.

1*(3^2)+2*(5^2)+3*(7^2)+…

Cosider: N=2 Here the total_sum= 1*(3^2)+2*(5^2).

      total_sum= 59.

Help the CodeMaster to find sum to N terms and Print Total_sum

QUICK EXPLANATION:

In this particular pattern we can use formula (n*(n+1) * ((6* n * n)+(14*n)+7))/6. Alse we can use simple for loop for finding Nth term of the series.

EXPLANATION:

we are given an integer N and we have to find the sum of the series upto Nth term.

Consider N=3, For N=3 total_sum will be 1(3^2)+2(5^2)+3(7^2).
Therefore Total_sum will be 206.

SOLUTIONS:

Setter's Solution

#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t–)
{
int n;
cin>>n;
long long total_sum=(n*(n+1) * ((6 * n* n)+(14*n)+7))/6;
cout<<total_sum<<endl;
}
return 0;
}

Tester's Solution

#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t–)
{
int n;
cin>>n;
long long total_sum=(n*(n+1) * ((6 * n* n)+(14*n)+7))/6;
cout<<total_sum<<endl;
}
return 0;
}

Editorialist's Solution

#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t–)
{
int n;
cin>>n;
long long total_sum=(n*(n+1) * ((6 * n* n)+(14*n)+7))/6;
cout<<total_sum<<endl;
}
return 0;
}