Home  >  Article  >  Backend Development  >  C/C++ program to calculate the sum of a sequence where the nth term is n raised to the power 2 minus (n-1) raised to the power 2

C/C++ program to calculate the sum of a sequence where the nth term is n raised to the power 2 minus (n-1) raised to the power 2

王林
王林forward
2023-09-08 20:45:02605browse

Here we will see how to calculate the sum of a series with n-th term n2 - (n-1)2. The recursive relationship is as follows -

Tn = n2 - (n−1)2

Therefore, The series is -

C/C++ program to calculate the sum of a sequence where the nth term is n raised to the power 2 minus (n-1) raised to the power 2

We need to find S mod (109 7) where S is the sum of all terms of the given series.

Example

#include<iostream>
#define X 1000000007
using namespace std;
long long getSum(long long n) {
   return ((n % X) * (n % X)) % X;
}
int main() {
   long long n = 56789;
   cout << getSum(n);
}

Output

224990500

The above is the detailed content of C/C++ program to calculate the sum of a sequence where the nth term is n raised to the power 2 minus (n-1) raised to the power 2. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete