首页 > 后端开发 > C++ > 正文

C/C++程序:计算以n的平方减去(n-1)的平方为第n项的序列的和

WBOY
发布: 2023-08-26 19:21:03
转载
753 人浏览过

C/C++程序:计算以n的平方减去(n-1)的平方为第n项的序列的和

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: 229137999
Output: 218194447
登录后复制

Explanation

Tn 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 = 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
登录后复制

Example

的中文翻译为:

示例

#include 
using namespace std;
#define mod 1000000007
int main() {
   long long n = 229137999;
   cout << ((n%mod)*(n%mod))%mod;
   return 0;
}
登录后复制

以上是C/C++程序:计算以n的平方减去(n-1)的平方为第n项的序列的和的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:tutorialspoint.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!