列印給定第 n 項之和的程式
問題陳述包括列印級數的和,其中給出了第 N 項。
輸入將給出 N 的值。我們需要找到序列直到 N 的和,其中序列的第 N 項由下式給出
$$\mathrm{N^{2}−(N−1)^{2}}$$
讓我們透過以下示例瞭解問題
輸入
N=5
輸出
25
解釋 - 給定的 N 值為 5。序列的前 5 項為
$\mathrm{N=1,1^{2}−(1−1)^{2}=1}$
$\mathrm{N=2,2^{2}−(2−1)^{2}=3}$
$\mathrm{N=3,3^{2}−(3−1)^{2}=5}$
$\mathrm{N=4,4^{2}−(4−1)^{2}=7}$
$\mathrm{N=5,5^{2}−(5−1)^{2}=9}$
直到第 5 項的序列項之和為 1+3+5+7+9=25。因此,輸出為 25。
輸入
N=8
輸出
64
解釋 - 使用序列第 N 項的公式計算直到第 8 項的序列和,
$\mathrm{N=1,1^{2}−(1−1)^{2}=1}$
$\mathrm{N=2,2^{2}−(2−1)^{2}=3}$
$\mathrm{N=3,3^{2}−(3−1)^{2}=5}$
$\mathrm{N=4,4^{2}−(4−1)^{2}=7}$
$\mathrm{N=5,5^{2}−(5−1)^{2}=9}$
$\mathrm{N=6,6^{2}−(6−1)^{2}=11}$
$\mathrm{N=7,7^{2}−(7−1)^{2}=13}$
$\mathrm{N=8,8^{2}−(8−1)^{2}=15}$
和將是 1+3+5+7+9+11+13+15=64,這是所需的輸出。
讓我們看看在 C++ 中解決問題的不同方法。
方法 1
該問題的樸素方法可以只是計算直到 N 的序列的每一項並將它們加起來以獲得直到 N 的序列的和,其中序列的第 N 項為 $\mathrm{N^{2}−(N−1)^{2}}$
在 C++ 中實現該方法以獲得直到 N 的序列和的步驟
我們將建立一個函式來計算直到其第 N 項的序列的和,其中序列的每一項由 $\mathrm{N^{2}−(N−1)^{2}}$ 給出
初始化一個變數來儲存 N 項序列的和。該變數必須是 long long 資料型別,只是為了確保 N 值較大的和的值。
在 for 迴圈中從 i=0 迭代到 i<=N 以計算 N 項序列的和。
對於每次迭代,我們將使用公式 $\mathrm{N^{2}−(N−1)^{2}}$ 將第 i 項的和新增到變數中,因為它表示序列的第 N 項。
迭代到 N 後返回變數,這將是前 N 項序列的和,其中 N 將是使用者輸入。
示例
//C++ code to find the sum of the sequence of first N terms where //Nth term is given by N^2-(N-1)^2 #include <bits/stdc++.h> using namespace std; //function to calculate the sum of the sequence until N long long sum(int N){ long long a=0; //to store the sum of the sequence //iterating to calculate every term of the sequence from 1 to N for(int i=1;i<=N;i++){ //using the formula N^2-(N-1)^2 a = a + (i * i - (i-1)*(i-1)); //adding each term of the sequence until N } return a; } int main() { int N; N=8; //input //calling the function cout<<"The sum of the sequence up to "<<N<<" terms is: "<<sum(N)<<endl; N=54; cout<<"The sum of the sequence up to "<<N<<" terms is: "<<sum(N)<<endl; return 0; }
輸出
The sum of the sequence up to 8 terms is: 64 The sum of the sequence up to 54 terms is: 2916
時間複雜度 - O(N),因為我們在 for 迴圈中迭代到 N 以計算序列的每一項並將其相加。
空間複雜度 - O(1),因為沒有佔用額外空間來查詢和。
方法 2(高效方法)
除了使用序列第 N 項的公式同時計算序列的每一項並將其相加外,我們還可以使用算術級數 (A.P.) 的 N 項和的概念,因為序列正在形成一個 A.P。
如果我們使用序列第 N 項的公式,即 $\mathrm{N^{2}−(N−1)^{2}}$ 計算序列的前幾項並觀察模式,我們得到
序列的第一個數字是 1。
序列的第二個數字將是 3。
同樣,$\mathrm{N=3,3^{2}−(3−1)^{2}=5}$
$\mathrm{N=4,4^{2}−(4−1)^{2}=7}$
$\mathrm{N=5,5^{2}−(5−1)^{2}=9}$
使用公式計算的序列的前幾個數字是 1, 3, 5, 7, 9, 11, 13……
我們可以看到,該序列只是一個 A.P.,其第一項為 1,公差為 2。
A.P. 的前 N 項之和由下式給出:
$\mathrm{S_{N}=\frac{N}{2}(2*a+(N−1)d)}$,其中 a=AP 的第一項,d=AP 的公差。
要計算直到 N 項的序列和,我們將使用上述公式。替換值後,我們得到
$\mathrm{序列和=\frac{N}{2}(2*1+(N−1)2)\:as\:(a=1\:and\:d=2)}$
$$\mathrm{=\frac{2*N}{2}(1+N−1)=N^{2}}$$
直到第 N 項的序列和,其中第 N 項由 $\mathrm{N^{2}−(N−1)^{2}}$ 給出,等於 N 本身的平方,其中 N 是項數。
在 C++ 中實現該方法的步驟
我們將建立一個函式來獲取直到第 N 項的序列的和,其第 N 項為 $\mathrm{N^{2}−(N−1)^{2}}$
初始化一個變數來儲存序列的和,並將其中 N 的平方的值儲存在其中,因為它給出了前 N 項序列的和。
返回將是所需輸出的變數。
示例
//C++ code to find the sum of the sequence of first N terms where //Nth term is given by N^2-(N-1)^2 #include <bits/stdc++.h> using namespace std; //function to calculate the sum of the sequence until N terms long long sum(int N){ long long a=0; //to store the sum of the sequence a = N*N; //the formula to give the sum of sequence up to N terms return a; } int main() { int N; N=75; //input //calling the function cout<<"The sum of the sequence up to "<<N<<" terms is: "<<sum(N)<<endl; N=1000; cout<<"The sum of the sequence up to "<<N<<" terms is: "<<sum(N)<<endl; return 0; }
輸出
The sum of the sequence up to 75 terms is: 5625 The sum of the sequence up to 1000 terms is: 1000000
時間複雜度 - O(1),花費恆定時間來計算序列的和。
空間複雜度 - O(1),因為沒有佔用額外空間。
結論
本文討論了查詢第 N 項為 $\mathrm{N^{2}−(N−1)^{2}}$ 的序列和的問題。我們討論了用樸素的方法解決問題的方法,以及在恆定時間和空間內在 C++ 中找到直到 N 項的序列和的高效解決方案。
我希望您在閱讀本文後能夠理解問題和解決問題的方法。