Nicomachus's Theorem Last Updated : 11 Jul, 2025 Comments Improve Suggest changes 3 Likes Like Report Nicomachus's Theorem states that sum of cubes of first n natural numbers is equal to squares of natural number sum.1^{3}+2^{3}+3^{3}+\cdots +n^{3}=\left(1+2+3+\cdots +n\right)^{2} In other words1^{3}+2^{3}+3^{3}+\cdots +n^{3}=\left(n*(n+1)/2)^{2} Or we can say that the sum is equal to square of n-th triangular number.Mathematical Induction based proof can be found here. C++ // CPP program to verify Nicomachus's Theorem #include <bits/stdc++.h> using namespace std; void NicomachusTheorem_sum(int n) { // Compute sum of cubes int sum = 0; for (int k=1; k<=n; k++) sum += k*k*k; // Check if sum is equal to // given formula. int triNo = n*(n+1)/2; if (sum == triNo * triNo) cout << "Yes"; else cout << "No"; } // driver function int main() { int n = 5; NicomachuTheorem_sum(n); return 0; } Java // Java program to verify Nicomachus's Theorem import java.io.*; class GFG { static void NicomachuTheorem_sum(int n) { // Compute sum of cubes int sum = 0; for (int k = 1; k <= n; k++) sum += k * k * k; // Check if sum is equal to // given formula. int triNo = n * (n + 1) / 2; if (sum == triNo * triNo) System.out.println("Yes"); else System.out.println("No"); } // driver function public static void main (String[] args) { int n = 5; NicomachuTheorem_sum(n); } } // This code is contributed by anuj_67. Python3 # Python3 program to verify # Nicomachus's Theorem def NicomachuTheorem_sum(n): # Compute sum of cubes sum = 0; for k in range(1, n + 1): sum += k * k * k; # Check if sum is equal to # given formula. triNo = n * (n + 1) / 2; if (sum == triNo * triNo): print("Yes"); else: print("No"); # Driver Code n = 5; NicomachuTheorem_sum(n); # This code is contributed # by mits C# // C# program to verify // Nicomachus's Theorem using System; class GFG { static void NicomachuTheorem_sum(int n) { // Compute sum of cubes int sum = 0; for (int k = 1; k <= n; k++) sum += k * k * k; // Check if sum is equal to // given formula. int triNo = n * (n + 1) / 2; if (sum == triNo * triNo) Console.WriteLine("Yes"); else Console.WriteLine("No"); } // Driver Code public static void Main () { int n = 5; NicomachuTheorem_sum(n); } } // This code is contributed by anuj_67 PHP <?php // PHP program to verify // Nicomachus's Theorem function NicomachuTheorem_sum($n) { // Compute sum of cubes $sum = 0; for ($k = 1; $k <= $n; $k++) $sum += $k * $k * $k; // Check if sum is equal to // given formula. $triNo = $n * ($n + 1) / 2; if ($sum == $triNo * $triNo) echo "Yes"; else echo "No"; } // Driver Code $n = 5; NicomachuTheorem_sum($n); // This code is contributed by anuj_67. ?> JavaScript <script> // JavaScript program to verify Nicomachus's Theorem function NicomachuTheorem_sum(n) { // Compute sum of cubes let sum = 0; for (let k = 1; k <= n; k++) sum += k * k * k; // Check if sum is equal to // given formula. let triNo = n * (n + 1) / 2; if (sum == triNo * triNo) document.write("Yes"); else document.write("No"); } // Driver code let n = 5; NicomachuTheorem_sum(n); // This code is contributed by souravghosh0416. </script> Output: Yes Time complexity : O(n) Auxiliary Space : O(1) Create Quiz Comment S Shashank_Pathak Follow 3 Improve S Shashank_Pathak Follow 3 Improve Article Tags : Mathematical DSA number-theory Explore DSA FundamentalsLogic Building Problems 2 min read Analysis of Algorithms 1 min read Data StructuresArray Data Structure 3 min read String in Data Structure 2 min read Hashing in Data Structure 2 min read Linked List Data Structure 2 min read Stack Data Structure 2 min read Queue Data Structure 2 min read Tree Data Structure 2 min read Graph Data Structure 3 min read Trie Data Structure 15+ min read AlgorithmsSearching Algorithms 2 min read Sorting Algorithms 3 min read Introduction to Recursion 15 min read Greedy Algorithms 3 min read Graph Algorithms 3 min read Dynamic Programming or DP 3 min read Bitwise Algorithms 4 min read AdvancedSegment Tree 2 min read Binary Indexed Tree or Fenwick Tree 15 min read Square Root (Sqrt) Decomposition Algorithm 15+ min read Binary Lifting 15+ min read Geometry 2 min read Interview PreparationInterview Corner 3 min read GfG160 3 min read Practice ProblemGeeksforGeeks Practice - Leading Online Coding Platform 1 min read Problem of The Day - Develop the Habit of Coding 5 min read Like