Make PHP print the result of the Factorial class

Asked

Viewed 637 times

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>

3 answers

2

It is not the answer of the question, but it can be an alternative for those looking for 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);
    }
}

2

For small numbers you can simply do:

array_product(range($numero, 1));

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

Test this.


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

class Fatorial
{

    function calcular($numero)
    {

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

    }

}

Soon:

$resultado = 1;

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

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

}

Test this.


If you need large numbers use the bcmath, once its output is by string.

0


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

Thus has the expected effect with all corrections:

<?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>
  • I believe that if you explain that the problem was only to store the return of the method and the validation of the existence of the value (if I understand correctly), it will greatly improve the answer. It’s that thing: always good to describe what happens in the code or, in this case, the correction that has been made. For a layman, these changes may not be so clear.

  • @Andersoncarloswoss made!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.