Nicomachu’s Theorem
Nicomachu’s Theorem states that sum of cubes of first n natural numbers is equal to squares of natural number sum.
![]()
In other words
![]()
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 Nicomachu's Theorem #include <bits/stdc++.h> using namespace std; void NicomachuTheorum_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; NicomachuTheorum_sum(n); return 0; } |
chevron_right
filter_none
Java
// Java program to verify Nicomachu's Theorem import java.io.*; class GFG { static void NicomachuTheorum_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; NicomachuTheorum_sum(n); } } // This code is contributed by anuj_67. |
chevron_right
filter_none
Python3
# Python3 program to verify # Nicomachu's Theorem def NicomachuTheorum_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; NicomachuTheorum_sum(n); # This code is contributed # by mits |
chevron_right
filter_none
C#
// C# program to verify // Nicomachu's Theorem using System; class GFG { static void NicomachuTheorum_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; NicomachuTheorum_sum(n); } } // This code is contributed by anuj_67 |
chevron_right
filter_none
PHP
<?php // PHP program to verify // Nicomachu's Theorem function NicomachuTheorum_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; NicomachuTheorum_sum($n); // This code is contributed by anuj_67. ?> |
chevron_right
filter_none
Output:
Yes
Recommended Posts:
- Fermat's little theorem
- Dilworth's Theorem
- Rosser's Theorem
- Fermat's Last Theorem
- Midy's theorem
- Wilson's Theorem
- Hardy-Ramanujan Theorem
- Euclid Euler Theorem
- Lagrange's four square theorem
- Compute nCr % p | Set 2 (Lucas Theorem)
- Corollaries of Binomial Theorem
- Compute nCr % p | Set 3 (Using Fermat Little Theorem)
- Extended Midy's theorem
- An application on Bertrand's ballot theorem
- Chinese Remainder Theorem | Set 1 (Introduction)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


