尼科馬庫斯定理
根據尼科馬庫斯定理,前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}$$
歸納假設
現在我們需要證明,如果P(k) 為真,其中k≥1,那麼邏輯上可以得出
$$\mathrm{P\lgroup k+1\rgroup 為真。}$$
所以這是我們的歸納假設
$$k^3=\lgroup k^2−k+1\rgroup+\lgroup k^2−k+3\rgroup+⋯+ \lgroup k^2+k−1\rgroup$$
然後我們需要證明:
$$\mathrm{\lgroup k+1\rgroup^{3}=\lgroup\lgroup k+1\rgroup^{2}- \lgroup k+1\rgroup+1\rgroup+\lgroup\lgroup k+1\rgroup{2}- \lgroup k+1\rgroup+3\rgroup+\dotso+\lgroup\lgroup k+1\rgroup ^{2}+\lgroup k+1\rgroup-1\rgroup}$$
歸納步驟
$$\mathrm{設 \:T_{k}=\lgroup k^{2}−k+1\rgroup+\lgroup k^{2}−k+3\rgroup+⋯+ \lgroup k^{2}+k−1\rgroup.}$$
我們可以將其表示為:
$$\mathrm{T_{k}=\lgroup k^{2}−k+1\rgroup+\lgroup k^{2}−k+3\rgroup+⋯+\lgroup k^{2}-k+2k−1\rgroup.}$$
我們看到Tk中有K項。
讓我們考慮Tk+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中,每一項都比T_k中對應的項大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^3}$$
$$\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,驗證n的尼科馬庫斯定理。如果定理成立,則列印“Yes”,否則列印“No”。
方法
為了驗證尼科馬庫斯定理,我們將首先計算立方和。然後我們將計算自然數之和。之後,我們將比較立方和與自然數之和的平方。
示例
$$\mathrm{對於\: n = 5} $$
$$\mathrm{立方和\colon 1^3 + 2^3 + 3^3 + 4^3 + 5^3 = 225}$$
$$\mathrm{自然數和\colon 1 + 2 + 3 + 4 + 5 = 15}$$
解決方案
為了計算自然數之和,我們將使用我們已經知道的公式,即:
$$\mathrm{前N個自然數之和= n*\lgroup n+1\rgroup/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
下面是一個驗證尼科馬庫斯定理的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
在這篇文章中,我們學習了尼科馬庫斯定理並對其進行了驗證。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP