Nicomachus’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 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)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!