Jump to content

Is cube number (?)


m1tch37

Recommended Posts

I need to function that finds if a number is a cube number. I have this at the moment:

 

function is_cube($n) {
$root = pow($n,1/3);
if ( intval($root) == $root )
	return true;
return false;
}

 

The problem, that i cant work out, is:

 

is_cube(8) returns true (As it should)

 

However is_cube(343) returns false. 73 is 343, so why on earth is it false?

 

What is even more confusing is in the case of 343, $root is float(7), but intval($root) is int(6). WHAT?

 

I dont get it...

 

Thanks

Mitch

Link to comment
https://forums.phpfreaks.com/topic/122988-is-cube-number/
Share on other sites

If speed's not a concern...

 

function is_cube($number) {
    if(!is_int($number)) {
        return;
    }

    for($i=0;$i<$number;$i++) {
        if(pow($i, 2) == sqrt($number)) {
            return true;
        } // Or just pow($i, 3) == $number. Whatever you want.
        return;
    }
}

 

----------------

Now playing: Breaking Benjamin - Breath

via FoxyTunes

Link to comment
https://forums.phpfreaks.com/topic/122988-is-cube-number/#findComment-635732
Share on other sites

pow() always returns a float, so is_int is useless :(.

 

 

You could do

 

function is_cube($number) {
    $p = pow($number, 1/3);
    return $p == ((int) $p);
}

 

 

It would be better to do it the way sasa did it though.  With this way, there's a tiny, tiny chance of floating point numbers screwing everything up.  (I don't think it would actually ever happen, but there's a tiny chance.)

 

 

On sasa's, contrary to what Ken said, it would always be accurate.  (Well, I can't think of when it wouldn't be accurate....  Could be wrong.)

Link to comment
https://forums.phpfreaks.com/topic/122988-is-cube-number/#findComment-635871
Share on other sites

If speed's not a concern...

 

function is_cube($number) {
    if(!is_int($number)) {
        return;
    }

    for($i=0;$i<$number;$i++) {
        if(pow($i, 2) == sqrt($number)) {
            return true;
        } // Or just pow($i, 3) == $number. Whatever you want.
        return;
    }
}

 

----------------

Now playing: Breaking Benjamin - Breath

via FoxyTunes

Can you use a return statement like that in PHP?

Link to comment
https://forums.phpfreaks.com/topic/122988-is-cube-number/#findComment-635884
Share on other sites

Yeah, it just returns null.  I personally would return false there though, since the number would not be cubic.  False would make more sense to me, but....

Ah cool. Thanks for the info mate. :)

 

m1tch37: also consider that cubic numbers have to be positive integers. :) I'm not sure if is_int() would cover that, but I don't think it does.

Link to comment
https://forums.phpfreaks.com/topic/122988-is-cube-number/#findComment-635910
Share on other sites

Ah DarkWater you're right.  I worded that horribly.  I meant if the fraction was odd (as in odd denominator) and the number was negative, and the numerator wouldn't make the number positive, the answer would be unreal.

 

 

Essentially xroot(n) where x is even.

 

 

Or, in fraction form, which is what I was trying to get at:

 

a^(b/c)

 

Where a is negative, b is odd, and c is even.

Link to comment
https://forums.phpfreaks.com/topic/122988-is-cube-number/#findComment-636260
Share on other sites

  • 2 weeks later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.