C++ 尼科马库斯定理

C++ 尼科马库斯定理

根据尼科马库斯定理,前n个整数的立方和等于第n个三角形数的平方。

或者我们也可以说 −

前n个自然数的立方和等于前n个自然数的和的平方。

代数表达式为,

\mathrm{\displaystyle\sum\limits_{i=0}^n i^3=\lgroup \frac{n^2+n}{2}\rgroup^2}

定理

1^3 = 1

2^3 = 3 + 5

3^3 = 7 + 9 + 11

4^3 = 13 + 15 + 17 + 19\vdots

泛化

n^3 =\lgroup n^2−n+1\rgroup+\lgroup n^2−n+3\rgroup+⋯+\lgroup n^2+n−1\rgroup

数学归纳法证明

对于所有n Ε 自然数,设P(n)为命题 −

n^3 =\lgroup n^2−n+1\rgroup+\lgroup n^2−n+3\rgroup+⋯+\lgroup n^2+n−1\rgroup

归纳基础

\mathrm{P\lgroup 1\rgroup: 是真实的,因为这只是说 1^{3}= 1}

我们可以将这个公式表示为:
\mathrm{T_{k}=\lgroup k^{2}-k+1\rgroup+\lgroup k^{2}-k+3\rgroup+⋯+\lgroup k^{2}-k+2k-1\rgroup.}
我们可以看到T中有K项 k.

让我们考虑T k+1 中的一般项((k+1) 2 -(k+1)+j)−

\mathrm{\lgroup k+1\rgroup^{2}-\lgroup k+1\rgroup+j=k^{2}+2k+1−\lgroup k+1\rgroup+j}

\mathrm{=k^{2}+j+2k}

所以,在Tk+1中,每一项都比Tk中的对应项大2k。

\mathrm{T^{k+1}= T^{k} +k\lgroup 2k\rgroup+ \lgroup k+1\rgroup^{2}+\lgroup k+1\rgroup−1}

\mathrm{= k^{3}+k\lgroup 2k\rgroup+\lgroup k+1\rgroup^{2}+\lgroup k+1\rgroup−1}

\mathrm{= k^3+2k^2+k^2+2k+1+k+1−1}

\mathrm{= k^3+3k^2+3k+1}

\mathrm{= \lgroup k+1\rgroup^2}

\mathrm{所以: P\lgroup k\rgroup \Rightarrow P\lgroup k+1\rgroup}

结论由数学归纳法原理得出。

因此

\mathrm{n^3 =\lgroup n^2-n+1\rgroup+\lgroup n^2-n+3\rgroup+⋯+\lgroup n^2+n-1\rgroup}

问题陈述

给定一个数字n,验证尼科马库斯定理是否成立。如果定理成立,则打印Yes,否则打印No。

方法

为了验证尼科马库斯定理,我们首先计算立方和。然后我们计算自然数的和。然后比较立方和自然数和的平方。

例子

\mathrm{对于: n = 5}

\mathrm{立方和:1^3 + 2^3 + 3^3 + 4^3 + 5^3 = 225}

\mathrm{自然数和:1 + 2 + 3 + 4 + 5 = 15}

解决方法

为了计算自然数的和,我们将使用我们已经知道的公式,即

\mathrm{前N个自然数的和 = n * (n+1)/2.}

让我们将其称之为 自然数之和。

为了计算立方和,我们将从一个初始值为0的变量开始。然后遍历所有的自然数,计算它们的立方,并将这些值加到变量中,让我们将其称之为 平方之和 .

然后,我们将计算得到的 立方和自然数之和 的平方进行比较。如果它们相等,那么尼科马库斯定理就被验证了。

伪代码

Start
sumOfCubes = 0;
For 1=< k <= n
sumOfCubes = sumOfCubes + k^3;
sumOfNatural= n * (n + 1) / 2;
If (sumOfNatural)^2 is equal to sumOfCubes
Then print Yes
Else Print No
End

示例 1

以下是一个验证Nicomachus’定理的C++程序−

#include <bits/stdc++.h>
using namespace std;
// Function to calculate the sum of cubes and to find the sum of natural numbers and then comparing them
void verifyTheorem(int n){
   // Initializing sum as 0
   int sumOfCubes = 0;
   // Iterating through natural numbers and adding their cubes to sum
   for (int k = 1; k <= n; k++){
      sumOfCubes += k * k * k;
   }
   // Check if sum is equal to given formula. Calculating the sum of natural numbers using the formula
   int naturalSum = n * (n + 1) / 2;
   // Comparing square of naturalSum to sumOfCubes
   if (sumOfCubes == naturalSum * naturalSum) {
      // Printing Yes if they are equal
      cout << "Yes";
   }
   else {
      // Printing No if they are not equal
      cout << "No";
   }
}

int main(){
   // Given value of n
   int n = 6;
   // Function call to verify theorem
   verifyTheorem(n);
   return 0;
}

输出

对于输入:i = 6,上述C++程序将产生以下输出 –

Yes

示例 2

我们可以通过将verify函数分解为多个函数来以更简洁的方式编写上述代码。

// Cpp program that verifies Nicomachus' Theorem
#include <bits/stdc++.h>
using namespace std;
// Function to return the sum of cubes of numbers from 1 to n
int calcsumOfCubes(int n){
   // Initializing sum as 0
   int sumOfCubes = 0;
   // Iterating through natural numbers and adding their cubes to sum
   for (int k = 1; k <= n; k++) {
      sumOfCubes += k * k * k;
   }
   return sumOfCubes;
}
// Calculating the sum of natural numbers using the formula
int calnaturalSum(int n){
   return n * (n + 1) / 2;
}
// Function to calculate the sum of cubes and to find the sum of natural numbersand then comparing them
void verifyTheorem(int n){
   // Function call to calculate sum of cubes
   int sumOfCubes = calcsumOfCubes(n);
   // Function call to calculate sum of natural numbers
   int naturalSum = calnaturalSum(n);
   // Comparing square of naturalSum to sumOfCubes
   if (sumOfCubes == naturalSum * naturalSum){
      // Printing Yes if they are equal
      cout << "Yes";
   }
   else {
      // Printing No if they are not equal
      cout << "No";
   }
}

int main()
{
   // Given value of n
   int n = 6;
   // Function call to verify theorem
   verifyTheorem(n);
   return 0;
}

输出

对于输入 i = 6,上述 C++ 程序会产生以下输出结果:

Yes

在本文中,我们学习了尼科马库斯的定理,并进行了验证。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程