Skip to content

Instantly share code, notes, and snippets.

@alibo
Created October 22, 2015 10:02
Show Gist options
  • Save alibo/fb1d35ab1f064c532e4a to your computer and use it in GitHub Desktop.
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!)
<?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;
@alibo
Copy link
Author

alibo commented Oct 22, 2015

Examples:

If we have 4 questions, the possibility of guessing at least 2 correct answers is:

$ php answer-possibility.php 4 2
Possibility: 0.2617187500000000000000000000000000000000000000000000000000000000000000

100 questions, at least 50 correct answers:

php answer-possibility.php 100 50
Possibility: 0.0000000663850248347665153516542686470914346302257991431899859403687471

100 questions, at least 20 correct answers:

php answer-possibility.php 100 20
Possibility: 0.9004695898946859448449903562740041725413774125137943152056172746750182

Please study hard instead of guessing!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment