> 백엔드 개발 > 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 학습자의 빠른 성장을 도와주세요!