there are many types of series in mathematics which can be solved easily in c programming. this program is to find the sum of following of series in c program.
tn = n2 - (n-1)2
find the sum of all of the terms of series as sn mod (109 + 7) and,
sn = t1 + t2 + t3 + t4 + ...... + tn
input: 229137999output: 218194447
explanationtn can be expressed as 2n-1 to get it
as we know ,
=> 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 = n2the 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
example 的中文翻译为:示例#include <iostream>using namespace std;#define mod 1000000007int main() { long long n = 229137999; cout << ((n%mod)*(n%mod))%mod; return 0;}
以上就是c/c++程序:计算以n的平方减去(n-1)的平方为第n项的序列的和的详细内容。