Created
November 21, 2017 22:57
-
-
Save MarineLB/8b39c9c8cb9cd9f422ec75c2ad82b224 to your computer and use it in GitHub Desktop.
Nombre magique - essai
This file contains 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 | |
$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