Find Cube Pairs | Set 2 ( A n ^ ( 1 / 3 ) Solution ) | Function to find pairs that can represent the given number as sum of two cubes ; find cube root of n ; create a array of size of size ' cubeRoot ' ; for index i , cube [ i ] will contain i ^ 3 ; Find all pairs in above sorted array cube whose sum is equal to n ; Driver Code
function findPairs ( n ) { var cubeRoot = parseInt ( Math . pow ( n , 1.0 / 3.0 ) ) ; var cube = Array . from ( { length : cubeRoot + 1 } , ( _ , i ) => 0 ) ; for ( i = 1 ; i <= cubeRoot ; i ++ ) cube [ i ] = i * i * i ; var l = 1 ; var r = cubeRoot ; while ( l < r ) { if ( cube [ l ] + cube [ r ] < n ) l ++ ; else if ( cube [ l ] + cube [ r ] > n ) r -- ; else { document . write ( " " + l + " " + r + " " ) ; l ++ ; r -- ; } } } var n = 20683 ; findPairs ( n ) ;