Home > Backend Development > C++ > 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

王林
Release: 2023-09-08 20:45:02
forward
746 people have browsed it

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);
}
Copy after login

Output

224990500
Copy after login

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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template