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/>";
?>