m1tch37 Posted September 6, 2008 Share Posted September 6, 2008 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( 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 Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted September 6, 2008 Share Posted September 6, 2008 What if you try to use the is_int() function? function is_cube($n) { return is_int(pow($n, 1/3)); } Quote Link to comment Share on other sites More sharing options...
sasa Posted September 7, 2008 Share Posted September 7, 2008 try <?php function is_cube($n) { $root = round(pow($n,1/3)); $root = (int) ($root * $root * $root); return $root == $n; } echo is_cube(343); ?> Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted September 7, 2008 Share Posted September 7, 2008 try <?php function is_cube($n) { $root = round(pow($n,1/3)); $root = (int) ($root * $root * $root); return $root == $n; } echo is_cube(343); ?> That MAY not be accurate. Quote Link to comment Share on other sites More sharing options...
LemonInflux Posted September 7, 2008 Share Posted September 7, 2008 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 Quote Link to comment Share on other sites More sharing options...
LemonInflux Posted September 7, 2008 Share Posted September 7, 2008 function is_cube($number) { return is_int(pow($number, 1/3)); } ? ---------------- Now playing: Breaking Benjamin - You via FoxyTunes Quote Link to comment Share on other sites More sharing options...
corbin Posted September 7, 2008 Share Posted September 7, 2008 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.) Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted September 7, 2008 Share Posted September 7, 2008 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? Quote Link to comment Share on other sites More sharing options...
corbin Posted September 7, 2008 Share Posted September 7, 2008 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.... Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted September 7, 2008 Share Posted September 7, 2008 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. Quote Link to comment Share on other sites More sharing options...
corbin Posted September 7, 2008 Share Posted September 7, 2008 The cube root of -27 is -3. For a = x^1/b where b is odd, if x < 0, a < 0. You may be thinking of even numbered exponents. If an exponent is even, and the number is negative, it will result in an unreal answer. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted September 7, 2008 Share Posted September 7, 2008 Not exactly, corbin. -3 ^ 2 = 9 You might be thinking of square roots of negative numbers, which is an imaginary number. sqrt(-36) = 6i Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted September 7, 2008 Share Posted September 7, 2008 Not exactly, corbin. -3 ^ 2 = 9 You might be thinking of square roots of negative numbers, which is an imaginary number. sqrt(-36) = 6i No, he's right. My bad. His equation was: a = b ^ 1 / c. So your equation is the same as: 6i = -36 ^ 1 / 2. Quote Link to comment Share on other sites More sharing options...
corbin Posted September 8, 2008 Share Posted September 8, 2008 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. Quote Link to comment Share on other sites More sharing options...
jordanwb Posted September 19, 2008 Share Posted September 19, 2008 What about this: function isCubed ($value) { $third_root = (int)pow ($value, 1/3); return ((int)pow ($third_root, 3)) == $value; } Quote Link to comment Share on other sites More sharing options...
corbin Posted September 20, 2008 Share Posted September 20, 2008 sasa's example earlier was essentially the same, except his(/her's?) used rounding instead of flooring. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.