求解级数 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