notepad++ 偶数行
The problem is we have a number N and we have to find sum of first N Even natural numbers.
问题是我们有一个数N ,我们必须找到前N个偶数自然数之和。
Example:
例:
Input:
n = 3
Output:
288 (2^3 + 4^3+6^3)
A simple solution is given below...
下面给出一个简单的解决方案 ...
Example 1:
范例1:
#include <iostream>
using namespace std;
int calculate(int n)
{
int sum = 0;
for (int i = 1; i <= n; i++)
sum = sum + (2*i) * (2*i) * (2*i);
return sum;
}
int main()
{
int num = 3;
cout<<"Number is = "<<num<<endl;
cout << "Sum of cubes of first "<<num<<" even number is ="<<calculate(num);
return 0;
}
Output
输出量
Number is = 3
Sum of cubes of first 3 even number is =288
The efficient approach is discussed below:
下面讨论了有效的方法 :
The sum of cubes of first n natural numbers is given by = (n*(n+1) / 2)^2
Sum of cubes of first n natural numbers can be written as...
= 2^3 + 4^3 + .... + (2n)^3
Now take out common term i.e 2^3
= 2^3 * (1^3 + 2^3 + .... + n^3)
= 2^3* (n*(n+1) / 2)^2
= 8 * ((n^2)(n+1)^2)/4
= 2 * n^2(n+1)^2
Now we can apply this formula directly to find the sum of cubes of first n even numbers.
现在我们可以直接应用此公式来找到前n个偶数的立方和 。
Example 2:
范例2:
#include <iostream>
using namespace std;
int calculate(int n)
{
int sum = 2 * n * n * (n + 1) * (n + 1);
return sum;
}
int main()
{
int num = 3;
cout<<"Number is = "<<num<<endl;
cout << "Sum of cubes of first "<<num<<" even number is ="<<calculate(num);
return 0;
}
Output
输出量
Number is = 3
Sum of cubes of first 3 even number is =288
翻译自: https://www.includehelp.com/cpp-programs/find-sum-of-cubes-of-first-n-even-numbers.aspx
notepad++ 偶数行