Perfect cubes in a range | Python3 code for Efficient method to print cubes between a and b ; An efficient solution to print perfect cubes between a and b ; Find cube root of both a and b ; Print cubes between acrt and bcrt ; Driver code
def cbrt ( n ) : NEW_LINE INDENT return ( int ) ( n ** ( 1. / 3 ) ) NEW_LINE DEDENT def printCubes ( a , b ) : NEW_LINE INDENT acrt = cbrt ( a ) NEW_LINE bcrt = cbrt ( b ) NEW_LINE for i in range ( acrt , bcrt + 1 ) : NEW_LINE INDENT if ( i * i * i >= a and i * i * i <= b ) : NEW_LINE INDENT print ( i * i * i , " ▁ " , end = " " ) NEW_LINE DEDENT DEDENT DEDENT a = 24 NEW_LINE b = 576 NEW_LINE print ( " Perfect ▁ cubes ▁ in ▁ given ▁ range : " ) NEW_LINE printCubes ( a , b ) NEW_LINE