Check whether a number can be represented as difference of two consecutive cubes | Java program for the above approach ; Function to print the two consecutive numbers whose difference is N ; Iterate in the range [ 0 , 10 ^ 5 ] ; Function to check if N is a perfect cube ; Find floating point value of square root of x . ; If square root is an integer ; Function to check whether a number can be represented as difference of two consecutive cubes ; Check if 12 * N - 3 is a perfect square or not ; Driver Code ; Given Number N
import java . util . * ; class GFG { static void print ( int N ) { for ( int i = 0 ; i < 100000 ; i ++ ) { if ( Math . pow ( i + 1 , 3 ) - Math . pow ( i , 3 ) == N ) { int j = i + 1 ; System . out . println ( i + " ▁ " + j ) ; return ; } } } static boolean isPerfectSquare ( double x ) { double sr = Math . sqrt ( x ) ; return ( ( sr - Math . floor ( sr ) ) == 0 ) ; } static boolean diffCube ( int N ) { return isPerfectSquare ( 12 * N - 3 ) ; } public static void main ( String [ ] args ) { int N = 19 ; if ( diffCube ( N ) ) { System . out . println ( " Yes " ) ; print ( N ) ; } else { System . out . println ( " No " ) ; } } }