Make PHP print the result of the Factorial class

1
<?php
    class Fatorial {

        function calcular(){

            $fat = $_GET['fat'];
            $resultado = 1;
            for($i = $fat; $i >= 1; $i--){
                $resultado *= $fat; 
                $fat--;

                return $resultado;
            }
        }
    }

    # Instancia a classe Fatorial()
    $c1 = new Fatorial();

    # Executa a função
    $c1->calcular();
?>

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8"/>
    <title>Questão 2</title>
</head>
<body>
    <form method="GET" action="">
        Digite um número:
        <input type="text" name="fat"><br>
        <input type="submit" value="enviar">
    </form  
</body>
</html>
    
asked by anonymous 22.05.2017 / 01:55

3 answers

0

Your current code has problems of various types, one is $_GET['fat'] that is added directly in the class method , this makes the code obsolete, a variable in the first parement of the method making the code useful at any time, another point is return within for , return in this case should be the last line, bringing the result correctly when all interactions are completed and finally it was necessary to give a echo to bring the output of the result.

That way it has the expected effect with all the fixes:

<?php
    class Fatorial 
    {    
        public function calcular($fat)
        {    
            $resultado = 1;
            for($i = $fat; $i > 1; $i--)
            {
                $resultado *= $fat;
                $fat--;                 
            }
            return $resultado;
        }
    }
    $result = "";
    if (isset($_GET['fat']) && is_numeric($_GET['fat']))
    {
        # Instancia a classe Fatorial()
        $c1 = new Fatorial();

        # Executa a função
        $result = $c1->calcular((int)$_GET['fat']);
    }

?>

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8"/>
    <title>Questão 2</title>
</head>
<body>
    <form method="GET" action="">
        Digite um número:
        <input type="text" name="fat"><br>
        <input type="submit" value="enviar">
    </form>
    <?php 
        if ($result != "")
        {
            echo $result;
        }
    ?>        
</body>
</html>
    
22.05.2017 / 03:20
2

For small numbers you can simply do:

array_product(range($numero, 1));

If you want 8! range() will create [8,7,6,5,4,3,2,1] and array_product will multiply all of them, resulting in 40320.

Try this.

Creating a class for this I believe is unnecessary, however:

class Fatorial
{

    function calcular($numero)
    {

        return array_product(range($numero, 1));

    }

}

Logo:

$resultado = 1;

if (isset($_GET['fat']) && ctype_digit($_GET['fat']) && $_GET['fat'] > 0) {

    $fatorial = new Fatorial();
    $resultado = $fatorial->calcular($_GET['fat']);

}

Try this.

If you need large numbers use bcmath , since your result is by string.

    
22.05.2017 / 05:09
2

It is not the answer to the question, but it can be an alternative for those who search by factorial. A very simple way: with gmp_fact

example - ideone

if (isset($_GET["fat"])){

    if ($_GET["fat"]==0){
        echo 1;
    }else{
        $fatorial = gmp_fact($_GET["fat"]);
        echo gmp_strval($fatorial);
    }
}
    
22.05.2017 / 03:56