# Number of perfect cubes between two given numbers

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

**Examples**:

Input: a = 3, b = 16Output: 1 The only perfect cube in given range is 8.Input: a = 7, b = 30Output: 2 The two cubes in given range are 8, and 27

**Method 1** : One naive approach is to check all the numbers between a and b (inclusive a and b) and increase count by one whenever we encounter a perfect cube.

Below is the implementation of above idea:

## CPP

`// A Simple Method to count cubes between a and b ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to count cubes between two numbers ` `int` `countCubes(` `int` `a, ` `int` `b) ` `{ ` ` ` `int` `cnt = 0; ` `// Initialize result ` ` ` ` ` `// Traverse through all numbers ` ` ` `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) ` ` ` `cnt++; ` ` ` ` ` `return` `cnt; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `a = 7, b = 30; ` ` ` `cout << ` `"Count of Cubes is "` ` ` `<< countCubes(a, b); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// A Simple Method to count cubes between a and b ` ` ` `class` `GFG{ ` ` ` `// Function to count cubes between two numbers ` `static` `int` `countCubes(` `int` `a, ` `int` `b) ` `{ ` ` ` `int` `cnt = ` `0` `; ` `// Initialize result ` ` ` ` ` `// Traverse through all numbers ` ` ` `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) ` ` ` `cnt++; ` ` ` ` ` `return` `cnt; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `a = ` `7` `, b = ` `30` `; ` ` ` `System.out.print(` `"Count of Cubes is "` ` ` `+ countCubes(a, b)); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# A Simple Method to count cubes between a and b ` ` ` `# Function to count cubes between two numbers ` `def` `countCubes(a, b): ` ` ` `cnt ` `=` `0` `# Initialize result ` ` ` ` ` `# Traverse through all numbers ` ` ` `for` `i ` `in` `range` `(a,b` `+` `1` `): ` ` ` ` ` `# Check if current number 'i' is perfect ` ` ` `# cube ` ` ` `for` `j ` `in` `range` `(i` `+` `1` `): ` ` ` `if` `j` `*` `j` `*` `j>i: ` ` ` `break` ` ` `if` `j ` `*` `j ` `*` `j ` `=` `=` `i: ` ` ` `cnt` `+` `=` `1` ` ` ` ` `return` `cnt ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `a ` `=` `7` ` ` `b ` `=` `30` ` ` `print` `(` `"Count of Cubes is "` `,countCubes(a, b)) ` ` ` `# This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

## C#

`// A Simple Method to count cubes between a and b ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to count cubes between two numbers ` `static` `int` `countCubes(` `int` `a, ` `int` `b) ` `{ ` ` ` `int` `cnt = 0; ` `// Initialize result ` ` ` ` ` `// Traverse through all numbers ` ` ` `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) ` ` ` `cnt++; ` ` ` ` ` `return` `cnt; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `a = 7, b = 30; ` ` ` `Console.Write(` `"Count of Cubes is "` ` ` `+ countCubes(a, b)); ` `} ` `} ` ` ` `// This code is conributed by chitranayal ` |

*chevron_right*

*filter_none*

**Output:**

Count of Cubes is 2

**Method 2 (Efficient)**: We can simply take cube root of ‘a’ and cube root of ‘b’ and Round cube root of ‘a’ up and cube root of ‘b’ down and count the perfect cubes between them using:

(floor(cbrt(b)) - ceil(cbrt(a)) + 1)We take floor of cbrt(b) because we need to consider numbers before b. We take ceil of cbrt(a) because we need to consider numbers after a. For example, let b = 28, a = 7. floor(cbrt(b)) = 3, ceil(cbrt(a)) = 2. And number of cubes is 3 - 2 + 1 = 2. The two numbers are 8 and 27.

Below is the implementation of above idea :

## C++

`// An Efficient Method to count cubes between a and b ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to count cubes between two numbers ` `int` `countCubes(` `int` `a, ` `int` `b) ` `{ ` ` ` `return` `(` `floor` `(cbrt(b)) - ` `ceil` `(cbrt(a)) + 1); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `a = 7, b = 28; ` ` ` `cout << ` `"Count of cubes is "` ` ` `<< countCubes(a, b); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// An Efficient Method to count cubes between a and b ` `class` `GFG ` `{ ` ` ` `// Function to count cubes between two numbers ` `static` `int` `countCubes(` `int` `a, ` `int` `b) ` `{ ` ` ` `return` `(` `int` `) (Math.floor(Math.cbrt(b)) - ` ` ` `Math.ceil(Math.cbrt(a)) + ` `1` `); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `a = ` `7` `, b = ` `28` `; ` ` ` `System.out.print(` `"Count of cubes is "` ` ` `+ countCubes(a, b)); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# An Efficient Method to count cubes between a and b ` `from` `math ` `import` `*` ` ` `# Function to count cubes between two numbers ` `def` `countCubes(a, b): ` ` ` ` ` `return` `(floor(b ` `*` `*` `(` `1.` `/` `3.` `)) ` `-` `ceil(a ` `*` `*` `(` `1.` `/` `3.` `)) ` `+` `1` `) ` ` ` `# Driver code ` `a ` `=` `7` `b ` `=` `28` `print` `(` `"Count of cubes is"` `,countCubes(a, b)) ` ` ` `# This code is contributed by shubhamsingh10 ` |

*chevron_right*

*filter_none*

## C#

`// An Efficient Method to count cubes between a and b ` `// C# implementation of the above approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to count cubes between two numbers ` `static` `int` `countCubes(` `int` `a, ` `int` `b) ` `{ ` ` ` `return` `(` `int` `) (Math.Floor(Math.Cbrt(b)) - ` ` ` `Math.Ceiling(Math.Cbrt(a)) + 1); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(` `string` `[] args) ` `{ ` ` ` `int` `a = 7, b = 28; ` ` ` `Console.WriteLine(` `"Count of cubes is "` `+ countCubes(a, b)); ` `} ` `} ` ` ` `// This code is contributed by Yash_R ` |

*chevron_right*

*filter_none*

**Output:**

Count of cubes is 2

**Time Complexity**: O(Log b).

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- How to swap two numbers without using a temporary variable?
- Write a program to reverse digits of a number
- Lucky Numbers
- Write an Efficient Method to Check if a Number is Multiple of 3
- Write an Efficient C Program to Reverse Bits of a Number
- Ugly Numbers
- Write a program to add two numbers in base 14
- Minimum number of jumps to reach end
- Find minimum number to be divided to make a number a perfect square
- Find whether a given number is a power of 4 or not
- Print all combinations of points that can compose a given number
- Count of Binary Digit numbers smaller than N
- Program for Fibonacci numbers
- Check if a number is multiple of 5 without using / and % operators
- Average of a stream of numbers
- Add two numbers without using arithmetic operators
- To find sum of two numbers without using any operator
- Count numbers that don't contain 3
- Given a number, find the next smallest palindrome
- Count the number of possible triangles

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.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.