Example #1
0
function insertCollatzes($high)
{
    global $conn;
    $statement = $conn->prepare('delete from collatz');
    $statement->execute();
    $values = "";
    $low = 1;
    $high = 100;
    for ($i = $low; $i < $high; $i += 2) {
        $n = $i;
        $collatzIndices = getCatIndices($n);
        $successor = $collatzIndices[0] * 3 - 1;
        if ($successor % 2 == 0) {
            --$successor;
        }
        $iterations = 13;
        $values .= "({$n}, {$collatzIndices['0']}, {$collatzIndices['1']}, {$successor}, {$iterations})";
        if ($i != $high - 1) {
            $values .= ", ";
        }
    }
    echo $values;
    $statement = $conn->prepare('
        insert into collatz(n, rowIndex, columnIndex, successor, iterations)
                     values ' . $values);
    $statement->execute();
}
Example #2
0
function insertCollatzes($amount)
{
    global $conn;
    $values = "";
    $low = maxCollatz() + 2;
    $high = $low + $amount - 1;
    for ($i = $low; $i < $high; $i += 2) {
        $n = $i;
        $collatzIndices = getCatIndices($n);
        $successor = $collatzIndices['row'] * 3 - 1;
        if ($successor % 2 == 0) {
            --$successor;
        }
        $iterations = 0;
        $functionName = getCatFunctionName(($n - 1) / 2);
        $formulaName = $functionName[0] . '' . $functionName[1];
        $formula = getCatFunctionFromName($functionName[0], $functionName[1]);
        $formulaStr = $formula[1] . ", " . $formula[3];
        $values .= "({$n}, " . $collatzIndices['row'] . ", " . $collatzIndices['column'] . ", {$successor}, {$iterations}, '{$formulaStr}', '{$formulaName}')";
        if ($i != $high - 1) {
            $values .= ", ";
        }
    }
    $statement = $conn->prepare('
        insert ignore into collatz(n, rowIndex, columnIndex, successor, iterations, formula, formulaName)
                     values ' . $values);
    $statement->execute();
}