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++
#include <iostream>
using namespace std;
int findS( int s)
{
int sum = 0;
for ( int n = 1; sum < s; n++)
{
sum += n * n * n;
if (sum == s)
return n;
}
return -1;
}
int main()
{
int s = 9;
int n = findS(s);
n == -1 ? cout << "-1" : cout << n;
return 0;
}
|
C
#include <stdio.h>
int findS( int s)
{
int sum = 0;
for ( int n = 1; sum < s; n++)
{
sum += n * n * n;
if (sum == s)
return n;
}
return -1;
}
int main()
{
int s = 9;
int n = findS(s);
n == -1 ? printf ( "-1" ) : printf ( "%d" ,n);
return 0;
}
|
Java
class GFG
{
static int findS( int s)
{
int sum = 0 ;
for ( int n = 1 ; sum < s; n++)
{
sum += n * n * n;
if (sum == s)
return n;
}
return - 1 ;
}
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
def findS (s):
_sum = 0
n = 1
while (_sum < s):
_sum + = n * n * n
n + = 1
n - = 1
if _sum = = s:
return n
return - 1
s = 9
n = findS (s)
if n = = - 1 :
print ( "-1" )
else :
print (n)
|
C#
using System;
class GFG
{
public static int findS( int s)
{
int sum = 0;
for ( int n = 1; sum < s; n++)
{
sum += n * n * n;
if (sum == s)
return n;
}
return -1;
}
static public void Main ( string []args)
{
int s = 9;
int n = findS(s);
if (n == -1)
Console.WriteLine( "-1" );
else
Console.WriteLine(n);
}
}
|
PHP
<?php
function findS( $s )
{
$sum = 0;
for ( $n = 1; $sum < $s ; $n ++)
{
$sum += $n * $n * $n ;
if ( $sum == $s )
return $n ;
}
return -1;
}
$s = 9;
$n = findS( $s );
if ( $n == -1)
echo ( "-1" );
else
echo ( $n );
?>
|
Javascript
<script>
function findS(s)
{
let sum = 0;
for (let n = 1; sum < s; n++)
{
sum += n * n * n;
if (sum == s)
return n;
}
return -1;
}
let s = 9;
let n = findS(s);
n == -1 ? document.write( "-1" ) :document.write(n);
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
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++
#include <bits/stdc++.h>
using namespace std;
int isTriangular( int num)
{
if (num < 0)
return false ;
int c = (-2 * num);
int b = 1, a = 1;
int d = (b * b) - (4 * a * c);
if (d < 0)
return -1;
float root1 = ( -b + sqrt (d)) / (2 * a);
float root2 = ( -b - sqrt (d)) / (2 * a);
if (root1 > 0 && floor (root1) == root1)
return root1;
if (root2 > 0 && floor (root2) == root2)
return root2;
return -1;
}
int isPerfectSquare( long double x)
{
long double sr = sqrt (x);
if ((sr - floor (sr)) == 0)
return floor (sr);
else
return -1;
}
int findS( int s)
{
int sr = isPerfectSquare(s);
if (sr == -1)
return -1;
return isTriangular(sr);
}
int main()
{
int s = 9;
int n = findS(s);
n == -1 ? cout << "-1" : cout << n;
return 0;
}
|
C
#include <stdio.h>
#include <stdbool.h>
#include <math.h>
int isTriangular( int num)
{
if (num < 0)
return false ;
int c = (-2 * num);
int b = 1, a = 1;
int d = (b * b) - (4 * a * c);
if (d < 0)
return -1;
float root1 = ( -b + sqrt (d)) / (2 * a);
float root2 = ( -b - sqrt (d)) / (2 * a);
if (root1 > 0 && floor (root1) == root1)
return root1;
if (root2 > 0 && floor (root2) == root2)
return root2;
return -1;
}
int isPerfectSquare( long double x)
{
long double sr = sqrt (x);
if ((sr - floor (sr)) == 0)
return floor (sr);
else
return -1;
}
int findS( int s)
{
int sr = isPerfectSquare(s);
if (sr == -1)
return -1;
return isTriangular(sr);
}
int main()
{
int s = 9;
int n = findS(s);
n == -1 ? printf ( "-1" ) : printf ( "%d" ,n);
return 0;
}
|
Java
class GFG
{
public static int isTriangular( int num)
{
if (num < 0 )
return 0 ;
int c = (- 2 * num);
int b = 1 , a = 1 ;
int d = (b * b) -
( 4 * a * c);
if (d < 0 )
return - 1 ;
double root1 = (-b +
Math.sqrt(d)) / ( 2 * a);
double root2 = (-b -
Math.sqrt(d)) / ( 2 * a);
if (( int )(root1) > 0 &&
( int )(Math.floor(root1)) ==
( int )(root1))
return ( int )(root1);
if (( int )(root2) > 0 &&
( int )(Math.floor(root2)) ==
( int )(root2))
return ( int )(root2);
return - 1 ;
}
static int isPerfectSquare( double x)
{
double sr = Math.sqrt(x);
if ((sr - Math.floor(sr)) == 0 )
return ( int )(Math.floor(sr));
else
return - 1 ;
}
static int findS( int s)
{
int sr = isPerfectSquare(s);
if (sr == - 1 )
return - 1 ;
return isTriangular(sr);
}
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
import math
def isTriangular(num):
if (num < 0 ):
return False ;
c = ( - 2 * num);
b = 1 ;
a = 1 ;
d = (b * b) - ( 4 * a * c);
if (d < 0 ):
return - 1 ;
root1 = ( - b + math.sqrt(d)) / / ( 2 * a);
root2 = ( - b - math.sqrt(d)) / / ( 2 * a);
if (root1 > 0 and
math.floor(root1) = = root1):
return root1;
if (root2 > 0 and
math.floor(root2) = = root2):
return root2;
return - 1 ;
def isPerfectSquare(x):
sr = math.sqrt(x);
if ((sr - math.floor(sr)) = = 0 ):
return math.floor(sr);
else :
return - 1 ;
def findS(s):
sr = isPerfectSquare(s);
if (sr = = - 1 ):
return - 1 ;
return int (isTriangular(sr));
s = 9 ;
n = findS(s);
if (n = = - 1 ):
print ( "-1" );
else :
print (n);
|
C#
using System;
class GFG
{
static int isTriangular( int num)
{
if (num < 0)
return 0;
int c = (-2 * num);
int b = 1, a = 1;
int d = (b * b) -
(4 * a * c);
if (d < 0)
return -1;
double root1 = (-b + Math.Sqrt(d)) / (2 * a);
double root2 = (-b - Math.Sqrt(d)) / (2 * a);
if (( int )(root1) > 0 &&
( int )(Math.Floor(root1)) == ( int )(root1))
return ( int )(root1);
if (( int )(root2) > 0 &&
( int )(Math.Floor(root2)) == ( int )(root2))
return ( int )(root2);
return -1;
}
static int isPerfectSquare( double x)
{
double sr = Math.Sqrt(x);
if ((sr - Math.Floor(sr)) == 0)
return ( int )(Math.Floor(sr));
else
return -1;
}
static int findS( int s)
{
int sr = isPerfectSquare(s);
if (sr == -1)
return -1;
return isTriangular(sr);
}
public static void Main()
{
int s = 9;
int n = findS(s);
if (n == -1)
Console.Write( "-1" );
else
Console.Write(n);
}
}
|
PHP
<?php
function isTriangular( $num )
{
if ( $num < 0)
return false;
$c = (-2 * $num );
$b = 1;
$a = 1;
$d = ( $b * $b ) - (4 * $a * $c );
if ( $d < 0)
return -1;
$root1 = (- $b + sqrt( $d )) /
(2 * $a );
$root2 = (- $b - sqrt( $d )) /
(2 * $a );
if ( $root1 > 0 &&
floor ( $root1 ) == $root1 )
return $root1 ;
if ( $root2 > 0 &&
floor ( $root2 ) == $root2 )
return $root2 ;
return -1;
}
function isPerfectSquare( $x )
{
$sr = sqrt( $x );
if (( $sr - floor ( $sr )) == 0)
return floor ( $sr );
else
return -1;
}
function findS( $s )
{
$sr = isPerfectSquare( $s );
if ( $sr == -1)
return -1;
return isTriangular( $sr );
}
$s = 9;
$n = findS( $s );
if ( $n == -1)
echo "-1" ;
else
echo $n ;
?>
|
Javascript
<script>
function isTriangular(num)
{
if (num < 0)
return 0;
var c = (-2 * num);
var b = 1, a = 1;
var d = (b * b) - (4 * a * c);
if (d < 0)
return -1;
var root1 = (-b + Math.sqrt(d)) / (2 * a);
var root2 = (-b - Math.sqrt(d)) / (2 * a);
if (parseInt( (root1)) > 0 && parseInt( (Math.floor(root1))) == parseInt( (root1)))
return parseInt(root1);
if (parseInt( (root2)) > 0 && parseInt( (Math.floor(root2))) == parseInt( (root2)))
return parseInt( (root2));
return -1;
}
function isPerfectSquare(x)
{
var sr = Math.sqrt(x);
if ((sr - Math.floor(sr)) == 0)
return parseInt( (Math.floor(sr)));
else
return -1;
}
function findS(s) {
var sr = isPerfectSquare(s);
if (sr == -1)
return -1;
return isTriangular(sr);
}
var s = 9;
var n = findS(s);
if (n == -1)
document.write("-1");
else
document.write(n);
</script>
|
Time Complexity: O(logn)
Auxiliary Space: O(1)