求解級數 1*2*3 + 2*3*4+ 3*4*5 + . . . + n*(n+1)*(n+2) 的和的程式
級數的和是指給定級數中所有項按照特定模式加在一起的值。這裡給定的模式是
∑ (n*(n+1)*(n+2)) 的形式,因為 (n*(n+1)*(n+2)) 是給定模式中的最後一項。以下文章詳細討論了三種方法來求解給定級數的和,並具有不同的時間和空間複雜度。
問題陳述
現在,讓我們看看如何計算級數 1*2*3 + 2*3*4 + 3*4*5 + . . . + n*(n+1)*(n+2) 的和。
示例
讓我們藉助示例來理解這個問題。
輸入
n = 12
輸出
8190
說明 −
1*2*3 + 2*3*4 + 3*4*5 + 4*5*6 + 5*6*7 + 6*7*8 + 7*8*9 + 8*9*10 + 9*10*11 + 10*11*12 + 11*12*13 + 12*13*14 = 6 + 24 + 60 + 120 + 210 + 336 + 504 + 720 + 990 + 1320 + 1716 + 2184 = 4290
輸入
n = 7
輸出
1260
說明 −
1*2*3 + 2*3*4 + 3*4*5 + 4*5*6 + 5*6*7 = 6 + 24 + 60 + 120 + 210 + 336 + 504 = 1260
輸入
n = 21
輸出
63756
說明 −
1*2*3 + 2*3*4 + 3*4*5 + 4*5*6 + 5*6*7 + . . . + 21*22*23 = 6 + 24 + 60 + 120 + 210 + 336 + 504 + . . . + 10626 = 63756
問題說明
讓我們嘗試理解這個問題並找到它的解決方案。我們有兩個選擇來解決上述問題。一個解決方案可以透過使用遞迴來完成,第二個解決方案可以透過使用迭代方法(藉助迴圈)來實現。
我們將逐一檢視這兩個解決方案。
方案 1
使用最後一項的暴力求解
使用表示式最後一項(即 n*(n+1)*(n+2))的遞迴程式碼
示例
以下是上述方法在各種程式語言中的實現:
#include<stdio.h>
#include<stdlib.h>
// Define a recursive function to calculate the sum of the series.
long long int SeriesSum(int n){
// Base case
if(n==0)return 0;
// store each value in a variable m
long long int m= (n*(n+1)*(n+2));
// making a recursive call
return m+ SeriesSum(n-1);
}
// main function
int main(){
int num=5;
// Declare a variable ‘num’ Call the function to get the output
printf("The sum of the series is: %lld", SeriesSum(num));
return 0;
}
輸出
The sum of the series is: 420
#include<bits/stdc++.h>
using namespace std;
// Define a recursive function to calculate the sum of the series.
long long int SeriesSum(int n){
// Base case
if(n==0)return 0;
// store each value in a variable m
long long int m= (n*(n+1)*(n+2));
// making a recursive call
return m+ SeriesSum(n-1);
}
// main function
int main(){
int num=5;
// Declare a variable ‘num’ Call the function to get the output
cout<< "The sum of the series is: " << SeriesSum(num);
return 0;
}
輸出
The sorted string as per ASCII values of the characters is: $%()7jkw
public class Main {
// Define a recursive function to calculate the sum of the series.
public static long seriesSum(int n) {
// Base case
if (n == 0) return 0;
// Calculate the value for the current term
long term = n * (n + 1) * (n + 2);
// Make a recursive call to calculate the sum of the remaining terms
return term + seriesSum(n - 1);
}
public static void main(String[] args) {
int num = 5; // Define the number of terms in the series
long result = seriesSum(num); // Call the function to get the output
System.out.println("The sum of the series is: " + result);
}
}
輸出
The sum of the series is: 420
#recursive function to calculate the sum of the series.
def SeriesSum(n):
# Base case
if n==0:
return 0
#store each value in a variable m
m= (n*(n+1)*(n+2))
#making a recursive call
return m + SeriesSum(n-1)
num=5
print("The sum of the series is:", SeriesSum(num));
輸出
The sum of the series is: 420
遞迴程式碼的複雜度
時間複雜度 - O(1);此程式碼執行固定次數的計算,與輸入的大小無關。
空間複雜度 - O(1);此程式碼使用固定數量的變數來儲存輸入值和結果,與輸入的大小無關。
迭代方法
迭代是一種技術,它涉及連續重複一定數量的步驟,直到滿足給定條件。
示例
以下是上述方法在各種程式語言中的實現:
#include<stdio.h>
int main(){
int num=5;
// Declare a variable ‘num’
long int ans=0;
// Iteration process for getting required answer using the last term
for(int i=1; i<=num; i++) {
ans += (i*(i+1)*(i+2));
}
// Print the output
printf("The sum of series is: %ld", ans);
return 0;
}
輸出
The sum of series is: 420
#include<bits/stdc++.h>
using namespace std;
int main(){
int num=5;
// Declare a variable ‘num’
long long int ans=0;
// Iteration process for getting required answer using the last term
for(int i=1; i<=num; i++) {
ans += (i*(i+1)*(i+2));
}
// Print the output
cout<< "The sum of series is: " << ans;
return 0;
}
輸出
The sum of series is: 420
public class Main {
public static void main(String[] args) {
int n = 5; // Define the number of terms in the series
long sum = 0; // Initialize the sum variable
for (int i = 1; i <= n; i++) {
// Calculate the i-th term of the series
long term = i * (i + 1) * (i + 2);
sum += term; // Add the term to the sum
}
System.out.println("The sum of the series is: " + sum);
}
}
輸出
The sum of series is: 420
num=5;
ans=0;
# Iteration process for getting required answer using the last term
for i in range (1, num+1):
ans += (i*(i+1)*(i+2))
# Print the output
print("The sum of series is:",ans)
輸出
The sum of series is: 420
迭代程式碼的複雜度
時間複雜度 - O(n);此程式碼執行固定次數的計算(迭代),這將取決於輸入,因為迴圈將執行 n 次,其中 n 是輸入。
空間複雜度 - O(1);此程式碼使用固定數量的變數來儲存輸入值和結果,與輸入的大小無關。
方案 2
使用可以由求和性質推匯出的公式的方法
我們可以透過使用求和性質和使用已知項的求和來推匯出公式。
要查詢公式 -
(n*(n+1)*(n+2)*(n+3))/4
1*2*3 + 2*3*4 + 3*4*5 + . . . + (n*(n+1)*(n+2))/4 => Σ (n*(n+1)*(n+2))/4 => Σ n*(n^2 + 3n +2) => Σ n^3 + Σ 3n^2 + Σ 2n . . . . . . . . . . . . . . . . . . . . . Equation 1
我們已經知道,
Σ n^3 = ((n*(n+1))/2) ^2; Σ n^2 = (n*(n+1)*(2n+1))/6; Σ n = (n*(n+1))/2;
將所有這些值代入公式 1,
=> (n*(n+1)^2/4 + (3 * (n*(n+1)*(2n+1))/6) + (2 * (n*(n+1))/2)
=> (n*(n+1))^2/4 + ((n*(n+1)*(2n+1))/2) + (2 * (n*(n+1))/2)
=> (n*(n+1))/2 * {(n*(n+1)/2) + (2n+1) + (2)}
=> (n*(n+1))/2 * {(n^2+n) + (4n+2) + (4))/2}
=> (n*(n+1))/4 * {(n^2 +5n+6)}
=> (n*(n+1))/4 * {(n+2)*(n+3)}
=> (n*(n+1)*(n+2)*(n+3))/4
因此,可以推匯出此公式來直接解決問題。
示例
以下是上述公式在各種程式語言中的實現:
#include <stdio.h>
// We just need to derive the formula, we can directly put the value of n in the formula to get our answer.
int SeriesSum(int n){
// Here to avoid the situation of an overflow of data we are doing step-wise calculations by dividing (n*(n+1)) and ((n+2)*(n+3)) separately by 2 as (n*(n+1)) must be divisible by 2 and ((n+2)*(n+3)) is also divisible by 2
return ((n * (n + 1))/2) * (((n + 2) * (n + 3))/ 2);
}
// main function
int main(){
int n=5;
printf("The number n is given as %d\n", n);
// Call the function to get the output
printf("The sum of the series is: %d\n", SeriesSum(n));
return 0;
}
輸出
The number n is given as 5 The sum of the series is: 420
#include <bits/stdc++.h>
using namespace std;
// We just need to derive the formula, we can directly put the value of n in the formula to get our answer.
int SeriesSum(int n){
// Here to avoid the situation of an overflow of data we are doing step-wise calculations by dividing (n*(n+1)) and ((n+2)*(n+3)) separately by 2 as (n*(n+1)) must be divisible by 2 and ((n+2)*(n+3)) is also divisible by 2
return ((n * (n + 1))/2) * (((n + 2) * (n + 3))/ 2);
}
// main function
int main(){
int n=5;
cout<< "The number n is given as "<< n << endl;
// Call the function to get the output
cout<< "The sum of the series is: " << SeriesSum(n);
return 0;
}
輸出
The number n is given as 5 The sum of the series is: 420
public class Main {
public static int SeriesSum(int n) {
// Here to avoid the situation of an overflow of data we are doing step-wise calculations by dividing (n*(n+1)) and ((n+2)*(n+3)) separately by 2 as (n*(n+1)) must be divisible by 2 and ((n+2)*(n+3)) is also divisible by 2
return ((n * (n + 1))/2) * (((n + 2) * (n + 3))/ 2);
}
// main function
public static void main(String[] args) {
int n=5;
System.out.println("The number n is given as "+ n);
// Call the function to get the output
System.out.println("The sum of the series is: "+ SeriesSum(n));
}
}
輸出
The number n is given as 5 The sum of the series is: 420
def SeriesSum(n):
# Here to avoid the situation of an overflow of data we are doing step-wise calculations by dividing (n*(n+1)) and ((n+2)*(n+3)) separately by 2 as (n*(n+1)) must be divisible by 2 and ((n+2)*(n+3)) is also divisible by 2
return int(((n * (n + 1)) / 2) * (((n + 2) * (n + 3)) / 2))
def main():
n = 5
print("The number n is given as", n)
# Call the function to get the output
print("The sum of the series is:", SeriesSum(n))
if __name__ == "__main__":
main()
輸出
The number n is given as 5 The sum of the series is: 420
以上程式碼的複雜度
時間複雜度 - O(1);此程式碼僅使用輸入 n 來執行計算。
空間複雜度 - O(1);此程式碼使用固定數量的變數來儲存輸入值和結果,與輸入的大小無關。
結論
在本文中,我們學習了三種不同的方法來解決同一個級數求和問題。我們還學習瞭如何使用求和性質直接推匯出級數求和的公式來編寫程式碼,從而節省時間和空間。我們還了解了如何避免輸入值問題中的溢位情況。
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP