Check whether a number can be represented as difference of two consecutive cubes | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void print ( int N ) { for ( int i = 0 ; i < 100000 ; i ++ ) { if ( pow ( i + 1 , 3 ) - pow ( i , 3 ) == N ) { cout << i << ' ▁ ' << i + 1 ; return ; } } } bool isPerfectSquare ( long double x ) { long double sr = sqrt ( x ) ; return ( ( sr - floor ( sr ) ) == 0 ) ; } bool diffCube ( int N ) { return isPerfectSquare ( 12 * N - 3 ) ; } int main ( ) { int N = 19 ; if ( diffCube ( N ) ) { cout << " Yes STRNEWLINE " ; print ( N ) ; } else { cout << " No STRNEWLINE " ; } return 0 ; }