Hey people... I have one problem...

Find how many total cubes are there in the range from A to B, inclusive.

An integer Y is total cube if it can be expressed as: Y = X*X*X, where X is also integer.
The first total cubes are: 1, 8, 27, 64 ...

Input parameters:
A, B - integer, the lower and the upper bounds.

Constraints:
A, B will have between 1 and 1000000 inclusive.
A will be less or equal to B (A<=B)

Return value:
int, the number of total cubes in the given range.

Class Name:
TotalCubes

Method signature:
public int howMany(int A, int B)

...and now... my class code is:

public int howMany(int A, int B) {

		if(A==B) { return 1; }

		int totCub = 0;

		for(double i=A; i<=B; i++) { 
                        // I try and with „int i...“
			if((Math.pow(i, 1.0/3.0)%10)==0) { totCub++; }
			}

		return totCub;
		}

...and the return value always is 0 (zero), why ? :(
Thanks...

Recommended Answers

All 7 Replies

Check out this Math.pow(8,1.0/3.0) does not give value 2.
use like this Math.pow(8,1.0/3).

sorry it gives i am working on it.....

The Math methods (Math.pow and Math.cbrt) works without any problem, but he uses double variables, and...

Math.pow(8, 1.0/3.0)%10 = 2.0 instead 0... or Math.cbrt(27)%10 = 3 instead 0...

How can I check if the decimal digit of the number is zero ?

i read in wikipedia and found 4 cube 64 is not a cube the list is 8,27,125 ...many others are giving a decimal values
this is how i counted the standard cubes upto 126

class cubes
{
  public static void main(String args[])
  {
boolean flag=false;	int count=0; int j;int i;
int A=5; int B=126;
for(i=2;i<=126;i++)
{
for(j=2;j<=100;j++)
{
if((Math.pow(i,1.0/3)==j))
{
count=count+1;
System.out.println("cube numbers are for "+j);
}
}
}
System.out.println(count);
}
}

check the output. Math.pow(64,1.0/3)=3.999
u can work out if u think i am wrong

u have to write a function to convert 3.99999 into 4 .Thats ur job now

Hey man... Thanks for your help... I solved the problem... here is the code:

public class TotalCubes {

public int howMany(int A, int B) {

if(A==B) { return 1; }

int totCub = 0;

for(int N=A; N<=B; N++) {
if(((Math.cbrt(N)%10)*10)%10==0) { totCub++; }
}

return totCub;
}
}

have u got the correct output.. if yes..good

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.