Nicomachus’ Theorem


According to Nicomachus’ Theorem, the sum of the cubes of the first n integers is equal to the square of the nth triangular number.

Or, we can also say −

The sum of cubes of first n natural numbers is equal to square of sum of first natural numbers.

Putting it algebraically,

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

Theorem

$$1^3 = 1$$

$$2^3 = 3 + 5$$

$$3^3 = 7 + 9 + 11$$

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

Generalizing

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

Proof By Induction

For all n Ε Natural Numbers, Let P(n) be the proposition −

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

Basis for the Induction

$$\mathrm{P\lgroup 1\rgroup\: is\: true,\: as\: this\: just\: says\: 1^{3}= 1}$$

Induction Hypothesis

Now we need to show that, if P(k) is true, where k≥1, then it logically follows that

$$\mathrm{P\lgroup k+1\rgroup is \:true.}$$

So this is our induction hypothesis

$$k^3=\lgroup k^2−k+1\rgroup+\lgroup k^2−k+3\rgroup+⋯+ \lgroup k^2+k−1\rgroup$$

Then we need to show −

$$\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}$$

Induction Step

$$\mathrm{Let \:T_{k}=\lgroup k^{2}−k+1\rgroup+\lgroup k^{2}−k+3\rgroup+⋯+ \lgroup k^{2}+k−1\rgroup.}$$

We can express this as −

$$\mathrm{T_{k}=\lgroup k^{2}−k+1\rgroup+\lgroup k^{2}−k+3\rgroup+⋯+\lgroup k^{2}-k+2k−1\rgroup.}$$

We see that there are K terms in Tk.

Let us consider the general term ((k+1)2−(k+1)+j) in Tk+1 −

$$\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}$$

So, in Tk+1, each of the terms is 2k larger than the corresponding term for T_k.

$$\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{So\: P\lgroup k\rgroup \Rightarrow P\lgroup k+1\rgroup}$$

And the result follows by the Principle of Mathematical Induction.

Therefore

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

Problem Statement

You are given a number n, Verify Nicomachus’ Theorem for n. Print Yes if the theorem is true, else print No.

Approach

To verify Nicomachus’ Theorem, we will first calculate the Sum of Cubes. Then we will calculate the Sum of Natural Numbers. After that we will compare the Sum of cubes and the square of Sum of Natural Numbers.

Example

$$\mathrm{For\: n = 5} $$

$$\mathrm{Sum \:of \:Cubes\colon 1^3 + 2^3 + 3^3 + 4^3 + 5^3 = 225}$$

$$\mathrm{Sum\: of\: Natural\: Numbers\colon 1 + 2 + 3 + 4 + 5 = 15}$$

Solution

To calculate the Sum of Natural Numbers we will be using the formula we already know, that is,

$$\mathrm{Sum\: of \:First\: N \:natural\: Numbers= n*\lgroup n+1\rgroup/2.}$$

Let’s call this Sum of Natural.

To calculate the Sum of Cubes we will start with a variable initiated with a value 0. Then iterate through all the natural numbers and calculate their cubes and add these values to the variable, let’s call this Sum of Squares.

Then we will compare the calculated Sum of Cubes with the square of Sum of Natural. If they come out to be equal, then the Nicomachus’ Theorem will be verified.

Pseudocode

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

Example 1

Below is a C++ program that verifies Nicomachus' Theorem −

#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;
}  

Output

For input: i = 6, the above C++ program will produce the following output −

Yes

Example 2

We can write the above code in a more clean way by dividing the verify function into multiple functions.

// 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;
}

Output

For input i = 6, the above C++ program, it will produce the following output −

Yes

In this article we learnt the Nicomachus’ theorem and verified it.

Updated on: 24-Aug-2023

118 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements