Help: cubing negative numbers (php)


(fxbg) #1

I suck at math, like really bad. I am working on a problem in PHP where I have some negative numbers and some positive numbers that will be summed together. I need to cube a negative number (by itself I found out…)

ex:
a = -3^-3 (which is -0)
b = 2^2 (is 4)

so that means: -0+4 = 4

but that’s wrong according to my test case, my test case says that should be 0 and not 4.

(note: all my test cases work except a test case with a negative number)

<?php
function cube_odd($a) 
{
        $ret = 0; 

        for($i=0;$i<count($a);$i++)
        {
                if(!is_int($a[$i])) return null;

                if($a[$i]%2!=0)
                {
                        $ret += pow($a[$i],$a[$i]);
                }
        }

        return $ret;
}

//28
$a = array(1, 2, 3, 4);
echo cube_odd($a)."<hr/>";

//0
$b = array(-3, -2, 2, 3);
echo cube_odd($b)."<hr/>";

//null
$c = array("a", 12, "z", 9);
die(var_dump(cube_odd($c)))."<hr/>";
?>


(CGMS) #2

Hello!
Your calculations are wrong:
For the second array ($b), only the first and the last items will be added to $ret, because of this validation: “$a[$i]%2!=0”.
So:
(-3)^(-3) = -0.03703703703
3^3 = 27
Sum of both = 26.962962962963


(fxbg) #3

No, that is incorrect, the test case says the answer should be 0. And yes, I am only calculating the odd numbers in the array.

– edit
I see my problem, my odd number detection (modulus) is saying that -2 is is odd… which of course it’s not. Damn, I looked right over that.

but -3^3+3^3 still comes out to 27, how can the test case be wrong?! Can you cube a float?


(CGMS) #4

I see my problem, my odd number detection (modulus) is saying that -2 is is odd… which of course it’s not. Damn, I looked right over that.

Here, -2 were identified correctly as even. Which version of PHP are you using?

but -3^3+3^3 still comes out to 27, how can the test case be wrong?! Can you cube a float?

-3^3 = -27 so, the above operation will result 0. And yes, we can cube a float.

I didn’t understand why you called your function “cube_odd” when it results x^x. You named it wrong or the logic is wrong?


(fxbg) #5
  1. for each odd number in the array, cube it.
  2. add the previously cubed numbers together.

ex; -3^-3+3^3 = 27

however my test case says it should be 0


(A Scrub) #6

um… I suck at math, but I suggest grab a calculator… lol. Double check your math. Your last comment for some reason doesn’t look right…


(CGMS) #7

There are some problems here, and I think you are getting lost with the negative numbers:

  1. Cube is x^3. Your program is calculating x^x. For cube, you should use pow($a[$i], 3);
  2. Fact is Minus 3 power Minus 3 equals -0.03703703703.
  3. Another fact: Minus 3 power 3 equals Minus 27.

Finally, I think my first observation will solve your problem.


(fxbg) #8

so basically this is a fail because i suck at maths. I thought cubing meant to do it by the same number, once i changed pow($a[$i], 3); it worked just fine and satisfied all test cases.

Thanks @CGMS, maybe I need to take a tour through some khan academy courses. My math is seriously fucked, like probably middle school (US). However, I can convert huge equations to code, so go figure lol

(uh and not to mention I ace all logic tests ever created)


(system) #9

This topic was automatically closed after 30 days. New replies are no longer allowed.