C / C ++程序,用于查找第n个项为n ^ 2 –(n-1)^ 2的级数之和

数学中有许多类型的级数,可以在C编程中轻松解决。该程序是在C程序中找到系列后继的和。

Tn = n2 - (n-1)2

找到所有序列项的总和,即Sn mod(10 9 + 7),然后,

S n = T 1 + T 2 + T 3 + T 4 + ...... + T n

Input: 229137999

Output: 218194447

说明

可以将Tn表示为2n-1

据我们所知 ,

=> Tn = n2 - (n-1)2

=>Tn = n2 - (1 + n2 - 2n)

=>Tn = n2 - 1 - n2 + 2n

=>Tn = 2n - 1.

find ∑Tn.

∑Tn = ∑(2n – 1)

Reduce the above equation to,

=>∑(2n – 1) = 2*∑n – ∑1

=>∑(2n – 1) = 2*∑n – n.

here, ∑n is the sum of first n natural numbers.

As known the sum of n natural number ∑n = n(n+1)/2.

Now the equation is,

∑Tn = (2*(n)*(n+1)/2)-n = n2

The value of n2 can be large. Instead of using n2 and take the mod of the result.

So, using the property of modular multiplication for calculating n2:

(a*b)%k = ((a%k)*(b%k))%k

示例

#include <iostream>

using namespace std;

#define mod 1000000007

int main() {

   long long n = 229137999;

   cout << ((n%mod)*(n%mod))%mod;

   return 0;

}

以上是 C / C ++程序,用于查找第n个项为n ^ 2 –(n-1)^ 2的级数之和 的全部内容, 来源链接: utcz.com/z/343647.html

回到顶部