CudoX
CudoX

Reputation: 1003

php - shifting variable value by if else simplification

I'm trying to make something like this to my variable data value...

$maxvalue = 0;
$basevalue = 0;
if($basevalue == 0) {$maxvalue = 0;}
else if ($basevalue == 1) {$maxvalue = 884;}
else if ($basevalue == 2) {$maxvalue = 1819;}
else if ($basevalue == 3) {$maxvalue = 2839;}

and so on.. i believe there is no exact computation on how the $maxvalue shifts as the basevalue increase. Can someone suggest me a simplier way to do this? thanks in advance!

Upvotes: 0

Views: 83

Answers (3)

CudoX
CudoX

Reputation: 1003

Here's the solution that prevented me putting all of them in an array..

$maxvalue = 17/6*(2*($basevalue*$basevalue*$basevalue)+3
($basevalue*$basevalue)+307*$basevalue);

Thanks for all the help

Upvotes: 0

GolezTrol
GolezTrol

Reputation: 116140

It looks like there's a pattern, almost like a faculty, but also with some other calculations. All numbers are multiples of 17. The following function returns the numbers you provided, so I think it might work for the higher numbers too:

function getMaxValue($base)
{
    // Factor of $base = 51 + $base^2 + Factor($base - 1). You
    // could solve that in a recursion, but a loop is generally better.
    $factor = 0;
    for ($i = 1; $i <= $base; $i++)
      $factor += 51 + ($i * $i);
    return $factor * 17;
}

// Test
for ($i = 0; $i < 100; $i++)
{
    echo "$i -- " . getMaxValue($i) . "<br>\n";
}

Upvotes: 1

deceze
deceze

Reputation: 522382

$maxvalues = array(0, 884, 1819, 2839, ...);
$maxvalue  = $maxvalues[$basevalue];

Upvotes: 6

Related Questions