<?php
function dblLinear($n) {
// your code
}
<?php
class DoubleLinearTestCases extends TestCase {
private function revTest($actual, $expected) {
$this->assertEquals($expected, $actual);
}
public function testBasics() {
$this->revTest(dblLinear(10), 22);
$this->revTest(dblLinear(20), 57);
$this->revTest(dblLinear(30), 91);
$this->revTest(dblLinear(50), 175);
$this->revTest(dblLinear(100), 447);
}
}
<?php
function dblLinear($n)
{
$i = 0;
$yArray = [];
$zArray = [];
$nextValue = 1;
while (true) {
if ($n === $i) {
return $nextValue;
}
$y = 2 * $nextValue + 1;
$yArray[] = $y;
$z = 3 * $nextValue + 1;
$zArray[] = $z;
$nextValue = min($yArray[0], $zArray[0]);
if ($nextValue === $yArray[0]) {
array_shift($yArray);
}
if ($nextValue === $zArray[0]) {
array_shift($zArray);
}
++$i;
}
}