function series_recursive($level)
{
    if ($level <= 3) {
        $a1 = pow(-1, rand(1, 2)) * rand(1, 4);
        // initial value of a1
        $a2 = 0;
        // initial value of a2
        $coeff_an_1 = pow(-1, rand(1, 2)) * rand(1, 3);
        // coefficient of a_{n-1} in recursive formula
        $coeff_an_2 = 0;
        // coefficient of a_{n-1} in recursive formula
        $coeff_n = 0;
        // coefficient of n in recursive formula
        $pow_n = 0;
        // power of n in recursive formula
        $coeff_a0 = pow(-1, rand(1, 2)) * rand(1, 5);
        // constant term in recursive formula
        $index = 2;
        // index of member we are looking for
        $formula = ' ' . $coeff_an_1 . '\\cdot a_{n-1}' . '+ ' . $coeff_a0;
        // recursive formula
        $init = '$a_1=' . $a1 . '$';
        // initial values
    } elseif ($level <= 6) {
        $a1 = pow(-1, rand(1, 2)) * rand(5, 10);
        $a2 = 0;
        $coeff_an_1 = pow(-1, rand(1, 2)) * rand(4, 7);
        $coeff_an_2 = 0;
        $coeff_n = pow(-1, rand(1, 2)) * rand(0, 2);
        $pow_n = rand(1, 3);
        $coeff_a0 = pow(-1, rand(1, 2)) * rand(5, 10);
        $index = rand(2, 3);
        $formula = ' ' . $coeff_an_1 . '\\cdot a_{n-1}' . '+ ' . $coeff_n . '\\cdot n^' . $pow_n . '+ ' . $coeff_a0;
        $init = '$a_1=' . $a1 . '$';
    } else {
        $a1 = pow(-1, rand(1, 2)) * rand(2, 4);
        $a2 = pow(-1, rand(1, 2)) * rand(1, 3);
        $coeff_an_1 = pow(-1, rand(1, 2)) * rand(4, 7);
        $coeff_an_2 = pow(-1, rand(1, 2)) * rand(2, 5);
        $coeff_n = pow(-1, rand(1, 2)) * rand(2, 5);
        $pow_n = rand(1, 5);
        $coeff_a0 = pow(-1, rand(1, 2)) * rand(5, 10);
        $index = rand(3, 5);
        $formula = ' ' . $coeff_an_1 . '\\cdot a_{n-1}' . '+ ' . $coeff_an_2 . '\\cdot a_{n-2}' . '+ ' . $coeff_n . '\\cdot n^' . $pow_n . '+ ' . $coeff_a0;
        $init = '$a_1=' . $a1 . '$ és $a_2=' . $a2 . '$';
    }
    $formula = str_replace(' 1\\cdot ', '', $formula);
    $formula = str_replace(' -1\\cdot ', ' -', $formula);
    $formula = str_replace('+ -', '-', $formula);
    $formula = str_replace('^1+', '+', $formula);
    $formula2 = str_replace('\\', '\\\\', $formula);
    $correct = recursiveSeries($a1, $a2, $coeff_an_1, $coeff_an_2, $coeff_n, $pow_n, $coeff_a0, $index);
    if ($correct > 9999) {
        $correct = number_format($correct, 0, ',', '\\,');
    }
    $an_1 = recursiveSeries($a1, $a2, $coeff_an_1, $coeff_an_2, $coeff_n, $pow_n, $coeff_a0, $index - 1);
    if ($index > 2 && $coeff_an_2 != 0) {
        $an_2 = recursiveSeries($a1, $a2, $coeff_an_1, $coeff_an_2, $coeff_n, $pow_n, $coeff_a0, $index - 2);
        if ($an_2 > 0) {
            $formula2 = str_replace('a_{n-2}', $an_2, $formula2);
        } else {
            $formula2 = str_replace('a_{n-2}', '(' . $an_2 . ')', $formula2);
        }
    }
    if ($an_1 > 0) {
        $formula2 = str_replace('a_{n-1}', $an_1, $formula2);
    } else {
        $formula2 = str_replace('a_{n-1}', '(' . $an_1 . ')', $formula2);
    }
    $formula2 = str_replace('n', $index, $formula2);
    $solution = '$' . $correct . '$ (mert $' . $formula2 . '=' . $correct . '$)';
    $question = 'Egy sorozat képzési szabálya a következő:$$a_n=' . $formula . '$$' . 'Ha tudjuk, hogy ' . $init . ', akkor mekkora $a_{' . $index . '}$ értéke?';
    return array('question' => $question, 'correct' => $correct, 'solution' => $solution);
}
Esempio n. 2
0
/**
 * Get member of series defined by recursive formula
 *
 * Formula: $coeff_an_1*a_{n-1} + $coeff_an_2*a_{n-2} + $coeff_n*n^{pow_n} + $coeff_a0
 *
 * @param int $a1         Initial value of a1
 * @param int $a2         Initial value of a2
 * @param int $coeff_an_1 Coefficient of a_{n-1} in recursive formula
 * @param int $coeff_an_2 Coefficient of a_{n-2} in recursive formula
 * @param int $coeff_n    Coefficient of n in recursive formula
 * @param int $pow_n      Power of n in recursive formula
 * @param int $coeff_a0   Constant term in recursive formula
 * @param int $index      Index of member we are looking for
 *
 * @return int $res Result
 */
function recursiveSeries($a1, $a2, $coeff_an_1, $coeff_an_2, $coeff_n, $pow_n, $coeff_a0, $index)
{
    if ($index == 1) {
        $res = $a1;
    } elseif ($index == 2 && $coeff_an_2 != 0) {
        $res = $a2;
    } else {
        $an_1 = recursiveSeries($a1, $a2, $coeff_an_1, $coeff_an_2, $coeff_n, $pow_n, $coeff_a0, $index - 1);
        if ($coeff_an_2 != 0) {
            $an_2 = recursiveSeries($a1, $a2, $coeff_an_1, $coeff_an_2, $coeff_n, $pow_n, $coeff_a0, $index - 2);
        } else {
            $an_2 = 0;
        }
        $res = $coeff_an_1 * $an_1 + $coeff_an_2 * $an_2 + $coeff_n * pow($index, $pow_n) + $coeff_a0;
    }
    return $res;
}