# Perfect cubes in a range

Given two given numbers a and b where 1 <= a <= b, find perfect cubes between a and b (a and b inclusive).

Examples:

```Input  :  a = 1, b = 100
Output : 1 8 27 64
Perfect cubes in the given range are
1, 8, 27, 64

Input :  a = 24, b = 576
Output : 27 64 125 216 343 512
Perfect cubes in the given range are
27, 64, 125, 216, 343, 512
```

## Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

This problem is similar to Perfect squares between two numbers.

Method 1 (Naive) : One naive approach is to check all the numbers between a and b (inclusive a and b)
and print the perfect cube. Following is the code for the above approach:

## C++

 `// A Simple Method to count cubes between a and b ` `#include ` `using` `namespace` `std; ` ` `  `void` `printCubes(``int` `a, ``int` `b) ` `{ ` `    ``// Traverse through all numbers in given range ` `    ``// and one by one check if number is prime ` `    ``for` `(``int` `i = a; i <= b; i++) { ` `        ``// Check if current number 'i' ` `        ``// is perfect cube ` `        ``for` `(``int` `j = 1; j * j * j <= i; j++) { ` `            ``if` `(j * j * j == i) { ` `                ``cout << j * j * j << ``"  "``; ` `                ``break``; ` `            ``} ` `        ``} ` `    ``} ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `a = 1, b = 100; ` `    ``cout << ``"Perfect cubes in given range:\n "``; ` `    ``printCubes(a, b); ` `    ``return` `0; ` `} `

## Java

 `// A Simple Method to count cubes between a and b ` ` `  `class` `Test { ` ` `  `    ``static` `void` `printCubes(``int` `a, ``int` `b) ` `    ``{ ` ` `  `        ``// Traverse through all numbers in given range ` `        ``// and one by one check if number is prime ` `        ``for` `(``int` `i = a; i <= b; i++) { ` ` `  `            ``// Check if current number 'i' ` `            ``// is perfect cube ` `            ``for` `(``int` `j = ``1``; j * j * j <= i; j++) { ` `                ``if` `(j * j * j == i) { ` `                    ``System.out.print(j * j * j + ``"  "``); ` `                    ``break``; ` `                ``} ` `            ``} ` `        ``} ` `    ``} ` `    ``// Driver method ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int` `a = ``1``, b = ``100``; ` `        ``System.out.println(``"Perfect cubes in given range:"``); ` `        ``printCubes(a, b); ` `    ``} ` `} `

## Python3

 `# A Simple Method to count cubes between a and b ` ` `  `def` `printCubes(a, b) : ` `    ``# Traverse through all numbers in given range ` `    ``# and one by one check if number is prime ` `    ``for` `i ``in` `range``(a, b ``+` `1``) : ` `         `  `        ``# Check if current number 'i' ` `        ``# is perfect cube ` `        ``j ``=` `1` `        ``for` `j ``in` `range``(j ``*``*` `3``, i ``+` `1` `) : ` `             `  `            ``if` `(j ``*``*` `3` `=``=` `i) : ` `                ``print``( j ``*``*` `3``, end ``=` `" "``) ` `                ``break` `             `  ` `  `# Driver code ` ` `  `a ``=` `1``; b ``=` `100` `print``(``"Perfect cubes in given range: "``) ` `printCubes(a, b) ` ` `  ` `  `# This code is contributed by Nikita Tiwari. `

## C#

 `// A Simple Method to count cubes ` `// between a and b ` `using` `System; ` ` `  `class` `GFG { ` ` `  `    ``static` `void` `printCubes(``int` `a, ``int` `b) ` `    ``{ ` ` `  `        ``// Traverse through all numbers ` `        ``// in given range and one by ` `        ``// one check if number is prime ` `        ``for` `(``int` `i = a; i <= b; i++) { ` ` `  `            ``// Check if current number 'i' ` `            ``// is perfect cube ` `            ``for` `(``int` `j = 1; j * j * j <= i; j++) { ` `                ``if` `(j * j * j == i) { ` `                    ``Console.Write(j * j * j + ``" "``); ` `                    ``break``; ` `                ``} ` `            ``} ` `        ``} ` `    ``} ` ` `  `    ``// Driver method ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int` `a = 1, b = 100; ` ` `  `        ``Console.WriteLine(``"Perfect cubes in"` `                          ``+ ``" given range:"``); ` `        ``printCubes(a, b); ` `    ``} ` `} ` ` `  `// This code contribute by parashar. `

## PHP

 ` `

Output :

```Perfect cubes in given range:
1 8 27 64
```

Method 2 (Efficient):
We can simply take cube root of ‘a’ and cube root of ‘b’ and print the cubes of number between them.

```1-  Given a = 24 b = 576

2-  acr = cbrt(a))  bcr = cbrt(b)
acr = 3 and bcr = 8

3-  Print cubes of 3 to 8 that comes under
the range of a and b(including a and b
both)
27, 64, 125, 216, 343, 512
```

Below is implementation of above steps.

## C++

 `// Efficient method to print cubes ` `// between a and b ` `#include ` `#include ` `using` `namespace` `std; ` ` `  `// An efficient solution to print perfect ` `// cubes between a and b ` `void` `printCubes(``int` `a, ``int` `b) ` `{ ` `    ``// Find cube root of both a and b ` `    ``int` `acrt = cbrt(a); ` `    ``int` `bcrt = cbrt(b); ` ` `  `    ``// Print cubes between acrt and bcrt ` `    ``for` `(``int` `i = acrt; i <= bcrt; i++) ` `        ``if` `(i * i * i >= a && i * i * i <= b) ` `            ``cout << i * i * i << ``" "``; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `a = 24, b = 576; ` `    ``cout << ``"Perfect cubes in given range:\n"` `         ``<< printCubes(a, b); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java progroam for Efficient method ` `// to print cubes between a and b ` ` `  `class` `Test { ` `    ``// An efficient solution to print perfect ` `    ``// cubes between a and b ` `    ``static` `void` `printCubes(``int` `a, ``int` `b) ` `    ``{ ` `        ``// Find cube root of both a and b ` `        ``int` `acrt = (``int``)Math.cbrt(a); ` `        ``int` `bcrt = (``int``)Math.cbrt(b); ` ` `  `        ``// Print cubes between acrt and bcrt ` `        ``for` `(``int` `i = acrt; i <= bcrt; i++) ` `            ``if` `(i * i * i >= a && i * i * i <= b) ` `                ``System.out.print(i * i * i + ``" "``); ` `    ``} ` ` `  `    ``// Driver method ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int` `a = ``24``, b = ``576``; ` `        ``System.out.println(``"Perfect cubes in given range:"``); ` `        ``printCubes(a, b); ` `    ``} ` `} `

## Python3

 `# Python3 code for Efficient method   ` `# to print cubes between a and b ` ` `  `def` `cbrt(n) : ` `    ``return` `(``int``)( n ``*``*` `(``1.` `/` `3``)) ` ` `  `# An efficient solution to print ` `# perfect cubes between a and b ` `def` `printCubes(a, b) : ` `     `  `    ``# Find cube root of  ` `    ``# both a and b ` `    ``acrt ``=` `cbrt(a) ` `    ``bcrt ``=` `cbrt(b) ` ` `  `    ``# Print cubes between acrt and bcrt ` `    ``for` `i ``in` `range``(acrt, bcrt ``+` `1``) : ` `        ``if` `(i ``*` `i ``*` `i >``=` `a ``and` `i ``*` `i ``*` `i <``=` `b) : ` `            ``print``(i ``*` `i ``*` `i, ``" "``, end ``=``"") ` ` `  `# Driver code ` `a ``=` `24` `b ``=` `576` `print``(``"Perfect cubes in given range:"``) ` `printCubes(a, b) ` ` `  ` `  `# This code is contributed  ` `# by Nikita Tiwari. `

## C#

 `// C# progroam for Efficient  ` `// method to print cubes  ` `// between a and b ` `using` `System; ` ` `  `class` `GFG ` `{ ` `    ``// An efficient solution ` `    ``// to print perfect ` `    ``// cubes between a and b ` `    ``static` `void` `printCubes(``int` `a,  ` `                           ``int` `b) ` `    ``{ ` `        ``// Find cube root of ` `        ``// both a and b ` `        ``int` `acrt = (``int``)Math.Pow(a,  ` `                             ``(``double``)1 / 3); ` `        ``int` `bcrt = (``int``)Math.Pow(b,  ` `                             ``(``double``)1 / 3); ` ` `  `        ``// Print cubes between ` `        ``// acrt and bcrt ` `        ``for` `(``int` `i = acrt;  ` `                 ``i <= bcrt; i++) ` `            ``if` `(i * i * i >= a &&  ` `                ``i * i * i <= b) ` `                ``Console.Write(i * i * ` `                              ``i + ``" "``); ` `    ``} ` ` `  `    ``// Driver Code ` `    ``static` `public` `void` `Main () ` `    ``{ ` `        ``int` `a = 24; ` `        ``int` `b = 576; ` `        ``Console.WriteLine(``"Perfect cubes "` `+  ` `                          ``"in given range:"``); ` `        ``printCubes(a, b); ` `    ``} ` `} ` ` `  `// This code is contributed  ` `// by ajit `

## PHP

 `= ``\$a` `&&  ` `            ``\$i` `* ``\$i` `* ``\$i` `<= ``\$b``) ` `                ``echo` `\$i` `* ``\$i` `* ``\$i` `, ``" "``; ` `} ` ` `  `// Driver code ` `\$a` `= 24; ``\$b` `= 576; ` `echo` `"Perfect cubes in given range:\n"``, ` `                    ``printCubes(``\$a``, ``\$b``); ` ` `  `// This code is contributed by ajit ` `?> `

Output:

```Perfect cubes in given range:
27 64 125 216 343 512
```

This article is contributed by Sahil Chhabra. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

My Personal Notes arrow_drop_up

Improved By : parashar, jit_t

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.