1

I have a site that requires random mathematical problems to be generated based on 3 parameters: operator (addition, subtraction, multiplication, division), regrouping (to carry or not to carry), and columns (one or two). I've been trying to wrap my head around how to go about doing this, but everything I come up with has a flaw in some way or other.

Here is what I am working with currently:

function create_problem($operator, $regrouping, $columns){
    $top = rand(1,9);
    $bottom = rand(1,9);

    if($operator == "+"){
        if($columns == 1 && $regrouping === false){
            $result = array(
                'top' => $top,
                'bottom' => $bottom,
                'formula' => "$top.$operator.$bottom"
            );
        }
        if($columns == 2){
            if($regrouping === false){
                if($top+$bottom > 9){
                    $diff = ($top+$bottom)-10;
                    $top = $diff+rand(1, 3);    
                }
                $result = array(
                    'top' => $top,
                    'bottom' => $bottom,
                    'formula' => "$top.$operator.$bottom"
                );
            }else{
                if($top+$bottom < 10){
                    $top = rand(1, $bottom+1);
                }
            }
        }
    }
    return $result;
}

If anyone has dealt with this, or if anyone has any pointers, I would be most appreciative!

1 Answer 1

2

this function first checks for regrouping, and produces random numbers so that the sum of common-column numbers are less than 10 (for no-carry) or greater or equal to 10 (for carry).

function create_problem($operator, $regrouping, $columns){
$x1 = '';
$x2 = '';
$y1 = '';
$y2 = '';
if ($regrouping){
    if ($columns == 2){
    $x1 = rand(1,9);
    $x2 = rand(0,9);
    $y1 = rand(9-$x1,9);
    $y2 = rand(10-$x2,9);
    } else {
    $x1 = rand(1,9);
    $y1 = rand(9-$x1,9);
    }
} else {
    if ($columns == 2){
    $x1 = rand(1,8);
    $x2 = rand(0,8);
    $y1 = rand(1,9-$x1);
    $y2 = rand(0,9-$x2);
    } else {
    $x1 = rand(1,8);
    $y1 = rand(1,9-$x1);
    }
}

return $x1.$x2.$operator.$y1.$y2;
}

ex..

31 +54

..where 3=$x1 1=$x2 5=$y1 4=$y2

Sign up to request clarification or add additional context in comments.

3 Comments

A simple test run looks great with two minor issues. 1) create_problem("-", false, 2); returns an output of 68-3-5 instead of a two column expression. 2) create_problem("-", true, 2); returns 67-40 which doesn't require regrouping. Looks great otherwise!
try again now with edits.. first error was a typo, second error was logic problem.
-1, you seem to have forgotten to explain what this is and why it works how it does. Code dumps are not answers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.