Perfect cubes in a range | 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
#include <cmath> NEW_LINE #include <iostream> NEW_LINE using namespace std ; void printCubes ( int a , int b ) { int acrt = cbrt ( a ) ; int bcrt = cbrt ( b ) ; for ( int i = acrt ; i <= bcrt ; i ++ ) if ( i * i * i >= a && i * i * i <= b ) cout << i * i * i << " ▁ " ; } int main ( ) { int a = 24 , b = 576 ; cout << " Perfect ▁ cubes ▁ in ▁ given ▁ range : STRNEWLINE " << printCubes ( a , b ) ; return 0 ; }