Skip to content

Instantly share code, notes, and snippets.

@MarineLB
Created November 21, 2017 22:57
Show Gist options
  • Save MarineLB/8b39c9c8cb9cd9f422ec75c2ad82b224 to your computer and use it in GitHub Desktop.
Save MarineLB/8b39c9c8cb9cd9f422ec75c2ad82b224 to your computer and use it in GitHub Desktop.
Nombre magique - essai
<?php
$min = 1;
$max = 1000;
$nbRandom = round(rand($min, $max));
$nbTest = null;
$nbTrouve = null;
$nbIterations = 0;
echo "Et le nombre gagnant est ".$nbRandom."...".PHP_EOL;
do{
$nbTest = round(($min+$max)/2);
if($nbRandom > $nbTest){
$min=$nbTest;
}else if($nbRandom < $nbTest){
$max=$nbTest;
}else{
$nbTrouve = $nbTest;
echo 'Le nombre magique est '.$nbTrouve.PHP_EOL;
}
$nbIterations++;
}while(!isset($nbTrouve));
echo "Trouvé en ".$nbIterations." itérations.".PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment