Given a number s (1 <= s <= 1000000000). If s is sum of the cubes of the first n natural numbers then print n, otherwise print -1.
First few Squared triangular number are 1, 9, 36, 100, 225, 441, 784, 1296, 2025, 3025, …
Examples :
Input : 9 Output : 2 Explanation : The given number is sum of cubes of first 2 natural numbers. 1*1*1 + 2*2*2 = 9 Input : 13 Output : -1
A simple solution is to one by one add cubes of natural numbers. If current sum becomes same as given number, then we return count of natural numbers added so far. Else we return -1.
C++
// C++ program to check if a // given number is sum of // cubes of natural numbers. #include <iostream> using namespace std; // Function to find if // the given number is // sum of the cubes of // first n natural numbers int findS(int s) { int sum = 0; // Start adding cubes of // the numbers from 1 for (int n = 1; sum < s; n++) { sum += n * n * n; // If sum becomes equal to s // return n if (sum == s) return n; } return -1; } // Driver code int main() { int s = 9; int n = findS(s); n == -1 ? cout << "-1" : cout << n; return 0; } |
Java
// Java program to check if // a given number is sum of // cubes of natural numbers. class GFG { // Function to find if // the given number is // sum of the cubes of // first n natural numbers static int findS(int s) { int sum = 0; // Start adding cubes of // the numbers from 1 for (int n = 1; sum < s; n++) { sum += n * n * n; // If sum becomes equal to s // return n if (sum == s) return n; } return -1; } // Drivers code public static void main(String[] args) { int s = 9; int n = findS(s); if (n == -1) System.out.println("-1"); else System.out.println(n); } } |
Python3
# Python3 program to find # if the given number is # sum of the cubes of first # n natural numbers # Function to find if the # given number is sum of # the cubes of first n # natural numbers def findS (s): _sum = 0 n = 1 # Start adding cubes of # the numbers from 1 while(_sum < s): _sum += n * n * n n += 1 n-= 1 # If sum becomes equal to s # return n if _sum == s: return n return -1 # Driver code s = 9n = findS (s) if n == -1: print("-1") else: print(n) |
C#
// C# program to check if a // given number is sum of // cubes of natural numbers. using System; class GFG { // Function to find if the // given number is sum of // the cubes of first n // natural numbers public static int findS(int s) { int sum = 0; // Start adding cubes of // the numbers from 1 for (int n = 1; sum < s; n++) { sum += n * n * n; // If sum becomes equal to s // return n if (sum == s) return n; } return -1; } // Driver code static public void Main (string []args) { int s = 9; int n = findS(s); if (n == -1) Console.WriteLine("-1"); else Console.WriteLine(n); } } // This code is contributed by Ajit. |
PHP
<?php // PHP program to check if // a given number is sum of // cubes of natural numbers. // Function to find if the // given number is sum of // the cubes of first n // natural numbers function findS($s) { $sum = 0; // Start adding cubes of // the numbers from 1 for ($n = 1; $sum < $s; $n++) { $sum += $n * $n * $n; // If sum becomes equal to s // return n if ($sum == $s) return $n; } return -1; } // Driver code $s = 9; $n = findS($s); if($n == -1) echo("-1"); else echo($n); // This code is contributed by Ajit. ?> |
2
An efficient solution is based on the formula [n(n+1)/2]2 for sum of first n cubes. We can see that all numbers are squares.
1) Check if given number is perfect square.
2) Check if square root is triangular (Please see method 2 of triangular numbers for this)
C++
// C++ program to check if a // given number is sum of // cubes of natural numbers. #include <bits/stdc++.h> using namespace std; // Returns root of n(n+1)/2 = num // if num is triangular (or integer // root exists). Else returns -1. int isTriangular(int num) { if (num < 0) return false; // Considering the equation // n*(n+1)/2 = num. The equation // is : a(n^2) + bn + c = 0"; int c = (-2 * num); int b = 1, a = 1; int d = (b * b) - (4 * a * c); if (d < 0) return -1; // Find roots of equation float root1 = ( -b + sqrt(d)) / (2 * a); float root2 = ( -b - sqrt(d)) / (2 * a); // checking if root1 is natural if (root1 > 0 && floor(root1) == root1) return root1; // checking if root2 is natural if (root2 > 0 && floor(root2) == root2) return root2; return -1; } // Returns square root of x if it is // perfect square. Else returns -1. int isPerfectSquare(long double x) { // Find floating point value of // square root of x. long double sr = sqrt(x); // If square root is an integer if ((sr - floor(sr)) == 0) return floor(sr); else return -1; } // Function to find if the given number // is sum of the cubes of first n // natural numbers int findS(int s) { int sr = isPerfectSquare(s); if (sr == -1) return -1; return isTriangular(sr); } // Driver code int main() { int s = 9; int n = findS(s); n == -1 ? cout << "-1" : cout << n; return 0; } |
Java
// Java program to check // if a given number is // sum of cubes of natural // numbers. // import java.Math.*; class GFG { // Returns root of n(n+1)/2 = num // if num is triangular (or // integer root exists). Else // returns -1. public static int isTriangular(int num) { if (num < 0) return 0; // Considering the equation // n*(n+1)/2 = num. The equation // is : a(n^2) + bn + c = 0"; int c = (-2 * num); int b = 1, a = 1; int d = (b * b) - (4 * a * c); if (d < 0) return -1; // Find roots of equation double root1 = (-b + Math.sqrt(d)) / (2 * a); double root2 = (-b - Math.sqrt(d)) / (2 * a); // checking if root1 is natural if ((int)(root1) > 0 && (int)(Math.floor(root1)) == (int)(root1)) return (int)(root1); // checking if // root2 is natural if ((int)(root2) > 0 && (int)(Math.floor(root2)) == (int)(root2)) return (int)(root2); return -1; } // Returns square root // of x if it is perfect // square. Else returns -1. static int isPerfectSquare(double x) { // Find floating point // value of square root of x. double sr = Math.sqrt(x); // If square root // is an integer if ((sr - Math.floor(sr)) == 0) return (int)(Math.floor(sr)); else return -1; } // Function to find if the // given number is sum of // the cubes of first n // natural numbers static int findS(int s) { int sr = isPerfectSquare(s); if (sr == -1) return -1; return isTriangular(sr); } // Driver code public static void main(String[] args) { int s = 9; int n = findS(s); if(n == -1) System.out.println("-1"); else System.out.println(n); } } // This code is contributed // by mits. |
Python3
# Python3 program to check # if a given number is sum of # cubes of natural numbers. import math # Returns root of n(n+1)/2 = num # if num is triangular (or integer # root exists). Else returns -1. def isTriangular(num): if (num < 0): return False; # Considering the equation # n*(n+1)/2 = num. The equation # is : a(n^2) + bn + c = 0"; c = (-2 * num); b = 1; a = 1; d = (b * b) - (4 * a * c); if (d < 0): return -1; # Find roots of equation root1 = (-b + math.sqrt(d)) // (2 * a); root2 = (-b - math.sqrt(d)) // (2 * a); # checking if root1 is natural if (root1 > 0 and math.floor(root1) == root1): return root1; # checking if root2 is natural if (root2 > 0 and math.floor(root2) == root2): return root2; return -1; # Returns square root of # x if it is perfect square. # Else returns -1. def isPerfectSquare(x): # Find floating point value # of square root of x. sr = math.sqrt(x); # If square root is an integer if ((sr - math.floor(sr)) == 0): return math.floor(sr); else: return -1; # Function to find if the given # number is sum of the cubes of # first n natural numbers def findS(s): sr = isPerfectSquare(s); if (sr == -1): return -1; return int(isTriangular(sr)); # Driver code s = 9; n = findS(s); if(n == -1): print("-1"); else: print(n); # This code is contributed by mits. |
C#
// C# program to check if a // given number is sum of // cubes of natural numbers. using System; class GFG { // Returns root of n(n+1)/2 = num // if num is triangular (or integer // root exists). Else returns -1. static int isTriangular(int num) { if (num < 0) return 0; // Considering the equation // n*(n+1)/2 = num. The equation // is : a(n^2) + bn + c = 0"; int c = (-2 * num); int b = 1, a = 1; int d = (b * b) - (4 * a * c); if (d < 0) return -1; // Find roots of equation double root1 = (-b + Math.Sqrt(d)) / (2 * a); double root2 = (-b - Math.Sqrt(d)) / (2 * a); // checking if root1 is natural if ((int)(root1) > 0 && (int)(Math.Floor(root1)) == (int)(root1)) return (int)(root1); // checking if root2 is natural if ((int)(root2) > 0 && (int)(Math.Floor(root2)) == (int)(root2)) return (int)(root2); return -1; } // Returns square root of x // if it is perfect square. // Else returns -1. static int isPerfectSquare(double x) { // Find floating point // value of square root of x. double sr = Math.Sqrt(x); // If square root // is an integer if ((sr - Math.Floor(sr)) == 0) return (int)(Math.Floor(sr)); else return -1; } // Function to find if the // given number is sum of // the cubes of first n // natural numbers static int findS(int s) { int sr = isPerfectSquare(s); if (sr == -1) return -1; return isTriangular(sr); } // Driver code public static void Main() { int s = 9; int n = findS(s); if(n == -1) Console.Write("-1"); else Console.Write(n); } } // This code is contributed by mits. |
PHP
<?php // PHP program to check if a // given number is sum of // cubes of natural numbers. // Returns root of n(n+1)/2 = num // if num is triangular (or integer // root exists). Else returns -1. function isTriangular($num) { if ($num < 0) return false; // Considering the equation // n*(n+1)/2 = num. The equation // is : a(n^2) + bn + c = 0"; $c = (-2 * $num); $b = 1; $a = 1; $d = ($b * $b) - (4 * $a * $c); if ($d < 0) return -1; // Find roots of equation $root1 = (-$b + sqrt($d)) / (2 * $a); $root2 = (-$b - sqrt($d)) / (2 * $a); // checking if root1 is natural if ($root1 > 0 && floor($root1) == $root1) return $root1; // checking if root2 is natural if ($root2 > 0 && floor($root2) == $root2) return $root2; return -1; } // Returns square root of // x if it is perfect square. // Else returns -1. function isPerfectSquare($x) { // Find floating point value // of square root of x. $sr = sqrt($x); // If square root is an integer if (($sr - floor($sr)) == 0) return floor($sr); else return -1; } // Function to find if the given // number is sum of the cubes of // first n natural numbers function findS($s) { $sr = isPerfectSquare($s); if ($sr == -1) return -1; return isTriangular($sr); } // Driver code $s = 9; $n = findS($s); if($n == -1) echo "-1"; elseecho $n; // This code is contributed by mits. ?> |
2
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Find the integers that doesnot ends with T1 or T2 when squared and added X
- Difference between Sum of Cubes and Sum of First N Natural Numbers
- Minimum value of K such that sum of cubes of first K natural number is greater than equal to N
- Check if a number can be represented as sum of two consecutive perfect cubes
- Check if a number can be represented as a sum of 2 triangular numbers
- First triangular number whose number of divisors exceeds N
- Find the sequence number of a triangular number
- Count pairs (a, b) whose sum of cubes is N (a^3 + b^3 = N)
- Sum of cubes of first n even numbers
- Sum of cubes of first n odd natural numbers
- Sum of cubes of even and odd natural numbers
- Numbers less than N that are perfect cubes and the sum of their digits reduced to a single digit is 1
- Sum of all Perfect Cubes lying in the range [L, R] for Q queries
- Sum of cubes of all Subsets of given Array
- Sum of alternating sign cubes of first N Natural numbers
- Sum of the series 1, 3, 6, 10... (Triangular Numbers)
- Program to count number of distinct Squares and Cubes upto N
- Number of perfect cubes between two given numbers
- Check whether a number can be represented as difference of two consecutive cubes
- Program to print triangular number series till n
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.

