Last active
September 2, 2020 21:27
-
-
Save gladson/a0dc4751c299883d1af1c037764ae8ad to your computer and use it in GitHub Desktop.
Fatorial e Permutacao
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Permutacao { | |
private $numero; | |
private $posicao; | |
private $resultado; | |
//Getters: | |
public function getNumero() { | |
return $this->numero; | |
} | |
public function getPosicao() { | |
return $this->posicao; | |
} | |
public function getResultado() { | |
return $this->resultado; | |
} | |
//Setters: | |
public function setNumero($num) { | |
$this->numero = $num; | |
} | |
public function setPosicao($posi) { | |
$this->posicao = $posi; | |
} | |
public function setResultado($result) { | |
$this->resultado = $result; | |
} | |
function fatorial($number) { | |
if ($number >= 2) { | |
// for ($number, $resultado = $number; $number >= 2; $number--) { | |
// $resultado = (($number-1) * $resultado); | |
// } | |
// return $resultado; | |
return array_product(range($number,1)); | |
} else { | |
return 1; | |
} | |
} | |
function combinacoes() { | |
$number = $this->getNumero(); | |
$posicao = $this->getPosicao(); | |
if ($number > $posicao) { | |
$combinacao = $this->fatorial($number) / $this->fatorial($number - $posicao); | |
return $combinacao; | |
} else { | |
return "ERRO: O número de elementos deve ser maior do que o de grupo"; | |
} | |
} | |
} | |
$teste = new Permutacao(); | |
$teste->setNumero(6); | |
$teste->setPosicao(3); | |
echo $teste->fatorial($teste->getNumero())."\n"; | |
echo $teste->combinacoes()."\n"; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment