<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Matemática Virtual, Ing.MSc. Fernando Carrillo & Ing.MSc. Cecilia Izurieta</title>
    <!-- Bootstrap CSS -->
    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
</head>
<body>
     <?php include 'encabezado.php'; ?>
    <main>
        <?php include 'menui.php'; ?>
    <div class="container mt-5">
        <h3 class="mb-4">Factor común por agrupación</h3>
    <h6 class="mb-4">Factorar en cada uno de los siguientes Ejercicios</h6>
        <div class="mt-4">
            <?php
                function generateBinomialMultiplication() {
                    $coefficients = range(1, 10);
                    $variables = ['x', 'y', 'z', 'a', 'b', 'c'];

                    // Generate random coefficients and variables for the terms
                    $a = $coefficients[array_rand($coefficients)];
                    $b = $coefficients[array_rand($coefficients)];
                    $c = $coefficients[array_rand($coefficients)];
                    $d = $coefficients[array_rand($coefficients)];
                    $var1 = $variables[array_rand($variables)];
                    $var2 = $variables[array_rand($variables)];

                    // Formulate the binomial multiplication expression
                    $expression = "($a$var1 + $b) \\times ($c$var2 + $d)";
                    
                    // Calculate the result using distributive property
                    if ($var1 == $var2) 
                    {
                    
                    $term1 = $a * $c . $var1 .'^2';
                    $term2 = $a * $d . $var1;
                    $term3 = $b * $c . $var2;
                    $term4 = $b * $d;
                    $result = "$term1 + $term2 + $term3 + $term4";
                      }
                    else
                    {
                    
                    $term1 = $a * $c . $var1 . $var2;
                    $term2 = $a * $d . $var1;
                    $term3 = $b * $c . $var2;
                    $term4 = $b * $d;
                    $result = "$term1 + $term3 + $term2 + $term4";
                    }    
                    

                    // Combine terms into a single result string
                    

                    return [$expression, $result];
                }

                for ($i = 1; $i <= $n; $i++) {
                    list($multiplication, $result) = generateBinomialMultiplication();
                    echo "<div class='card mb-3'>";
                    echo "<div class='card-body'>";
                    echo "<h5 class='card-title'>Ejercicio $i</h5>";
                    //echo "<p class='card-text'>Multiplicación: \( $multiplication \)</p>";
                    echo "<p class='card-text'> \( $result \)</p>";
                    echo "</div>";
                    echo "</div>";
                }
            ?>
        </div>
    </div>
    <!-- MathJax -->
    <script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
    <script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
</body>
 <?php include 'subir.php'; ?>
</html>
