Created
October 22, 2015 10:02
-
-
Save alibo/fb1d35ab1f064c532e4a to your computer and use it in GitHub Desktop.
Calculate the possibility of minimum correct answers in single-choice questions with 4 choices (by just guessing!)
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 | |
bcscale(70); | |
function factorial($number){ | |
$factorial = 1; | |
while($number > 0){ | |
$factorial = bcmul($factorial, $number); | |
$number--; | |
} | |
return $factorial; | |
} | |
function compound($select, $total) { | |
return bcdiv(factorial($total), bcmul(factorial($select), factorial($total - $select))); | |
} | |
function possibility($total, $minimum) { | |
$sum = 0; | |
for($i = $minimum; $i <= $total; $i++){ | |
$sum = bcadd( | |
bcmul( | |
bcmul( | |
compound($i, $total), | |
bcdiv(1, bcpow(4, $i)) | |
), | |
bcpow(bcdiv(3, 4), $total - $i) | |
), | |
$sum | |
); | |
} | |
return $sum; | |
} | |
$total = $argv[1]; | |
$minimum = $argv[2]; | |
$possibility = possibility($total, $minimum); | |
echo "Possibility: " . possibility($total, $minimum) . PHP_EOL; | |
echo "Point: " . ((($minimum * 3) - ($total - $minimum)) * 100) / ($total * 3) . PHP_EOL; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Examples:
If we have 4 questions, the possibility of guessing at least 2 correct answers is:
100 questions, at least 50 correct answers:
100 questions, at least 20 correct answers:
Please study hard instead of guessing!!