Problem:
We are interested in triangles that have integer length sides, all of which are between
minLengthandmaxLength, inclusive. How many such triangles are there? Two triangles differ if they have a different collection of side lengths, ignoring order. Triangles with side lengths {2,3,4} and {4,3,5} differ, but {2,3,4} and {4,2,3} do not. We are only interested in proper triangles; the sum of the two smallest sides of a proper triangle must be strictly greater than the length of the biggest side.Create a class
TriCountthat contains a methodcountthat is given intsminLengthandmaxLengthand returns the number of different proper triangles whose sides all have lengths betweenminLengthandmaxLength, inclusive. If there are more than 1,000,000,000, return -1.
My solution:
class Form{
/**
*@var array données utilisées par le formulaire
*/
protected $data;
/**
*@var string tag qui entoure les champs
*/
public $surroud ='p';
/**
*@param array $data
*@return string
*/
public function __construct($data = array()){
$this->data = $data;
}
/**
*@param $html string
*@return string
*/
protected function surroud(string $html){
return "<{$this->surroud}>".$html."</{$this->surroud}>";
}
/**
*@param $index string
*@return string
*/
protected function getValue(string $index){
return isset($this->data[$index]) ? $this->data[$index] : null;
}
/**
*@param $name string
*@return string
*/
public function input(string $name){
return $this->surroud("<label for='".$name."'>".$name.": </label><input type='text' name='".$name."' value='".$this->getValue($name)."'>");
}
/**
*@return string
*/
public function submit(){
return $this->surroud("<button type='submit'>Envoyer</button>");
}
}
<?php
class FormController{
/**
*@return objet
*/
public function registerI()
{
return new TriCount();
}
/**
*@param $params array
*@return integer
*/
public function register(array $params)
{
//les champs sont remplis d'entier
if(intval($params['min']) && intval($params['max'])){
//instancier la classe pour le calcul des probabilités
$inst = new TriCount();
//appel de la methode qui calcul les probabilités
$nbre = $inst->count($params['min'], $params['max']);
return $nbre;
}else{
$message_erreur = "Vous devez remplir avec des entiers superieur à 0!";
return $message_erreur;
}
}
}
<?php
/**
*Class TriCount
*/
class TriCount{
/**
*@var integer minimum du tableau
*/
private $minLength;
/**
*@var integer maximum du tableau
*/
private $maxLength;
/**
*@var integer nombre de triangle possible
*/
private $count;
/**
*@param $minLength integer
*@param $maxLength integer
*@return integer
*/
public function count(int $minLength , int $maxLength ){
//initialiser le compteur
$count = 0;
//3 boucles qui font varier le (i,j,k)
// le script s'arrete si la condition n'est pas vérifiée
for ($i = $minLength; $i <= $maxLength; $i++){
for($j = $i ; $j <= $maxLength; $j++){
for($k = $j ; $k <= $maxLength; $k++){
//condition: la somme des deux petits cotés du triangle superieur au troisieme coté
if( ($i + $j ) > $k ) {
$count++;
}else{
break;
}
}
}
}
//si le nombre de possibilité dépasse 1000000000
if ($count <= 1000000000 ){
return $count;
}else {
return -1;
}
}
}
