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 = 16
Output : 1
The only perfect cube in given range is 8.
Input : a = 7, b = 30
Output : 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
#include <bits/stdc++.h>
using namespace std;
int countCubes( int a, int b)
{
int cnt = 0;
for ( int i = a; i <= b; i++)
for ( int j = 1; j * j * j <= i; j++)
if (j * j * j == i)
cnt++;
return cnt;
}
int main()
{
int a = 7, b = 30;
cout << "Count of Cubes is "
<< countCubes(a, b);
return 0;
}
|
Java
class GFG{
static int countCubes( int a, int b)
{
int cnt = 0 ;
for ( int i = a; i <= b; i++)
for ( int j = 1 ; j * j * j <= i; j++)
if (j * j * j == i)
cnt++;
return cnt;
}
public static void main(String[] args)
{
int a = 7 , b = 30 ;
System.out.print( "Count of Cubes is "
+ countCubes(a, b));
}
}
|
Python3
def countCubes(a, b):
cnt = 0
for i in range (a,b + 1 ):
for j in range (i + 1 ):
if j * j * j>i:
break
if j * j * j = = i:
cnt + = 1
return cnt
if __name__ = = '__main__' :
a = 7
b = 30
print ( "Count of Cubes is " ,countCubes(a, b))
|
C#
using System;
class GFG{
static int countCubes( int a, int b)
{
int cnt = 0;
for ( int i = a; i <= b; i++)
for ( int j = 1; j * j * j <= i; j++)
if (j * j * j == i)
cnt++;
return cnt;
}
public static void Main()
{
int a = 7, b = 30;
Console.Write( "Count of Cubes is "
+ countCubes(a, b));
}
}
|
Javascript
<script>
function countCubes(a, b)
{
let cnt = 0;
for (let i = a; i <= b; i++)
for (let j = 1; j * j * j <= i; j++)
if (j * j * j == i)
cnt++;
return cnt;
}
let a = 7, b = 30;
document.write( "Count of Cubes is " + countCubes(a, b));
</script>
|
Output:
Count of Cubes is 2
Time Complexity: O((b – a)4/3)
Auxiliary Space: O(1)
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++
#include <bits/stdc++.h>
using namespace std;
int countCubes( int a, int b)
{
return ( floor (cbrt(b)) - ceil (cbrt(a)) + 1);
}
int main()
{
int a = 7, b = 28;
cout << "Count of cubes is "
<< countCubes(a, b);
return 0;
}
|
Java
class GFG
{
static int countCubes( int a, int b)
{
return ( int ) (Math.floor(Math.cbrt(b)) -
Math.ceil(Math.cbrt(a)) + 1 );
}
public static void main(String[] args)
{
int a = 7 , b = 28 ;
System.out.print( "Count of cubes is "
+ countCubes(a, b));
}
}
|
Python3
from math import *
def countCubes(a, b):
return (floor(b * * ( 1. / 3. )) - ceil(a * * ( 1. / 3. )) + 1 )
a = 7
b = 28
print ( "Count of cubes is" ,countCubes(a, b))
|
C#
using System;
class GFG
{
static int countCubes( int a, int b)
{
return ( int ) (Math.Floor(Math.Cbrt(b)) -
Math.Ceiling(Math.Cbrt(a)) + 1);
}
public static void Main( string [] args)
{
int a = 7, b = 28;
Console.WriteLine( "Count of cubes is " + countCubes(a, b));
}
}
|
Javascript
<script>
function countCubes(a, b){
return (Math.floor(b **(1./3.)) - Math.ceil(a **(1./3.)) + 1)
}
let a = 7;
let b = 28;
document.write( "Count of cubes is " +countCubes(a, b))
</script>
|
Output:
Count of cubes is 2
Time Complexity: O(Log b).
Auxiliary Space: O(1)
Last Updated :
26 Nov, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...