Skip to content

Commit

Permalink
refactor: gcd function
Browse files Browse the repository at this point in the history
  • Loading branch information
kolotov committed Sep 4, 2023
1 parent 4a326bf commit 541dcbb
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
}
],
"require": {
"wp-cli/php-cli-tools": "^0.11.19",
"ext-gmp": "*"
"wp-cli/php-cli-tools": "^0.11.19"
},
"bin": [
"bin/brain-games",
Expand Down
9 changes: 6 additions & 3 deletions src/Games/GcdGame.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@
return function ($module) {
$module[LOCATION_SETTINGS][SETTING_RULES] = 'Find the greatest common divisor of given numbers.';

$module[LOCATION_HANDLERS][HANDLER_QUESTION] = function () {
$gcd = function ($a, $b) use (&$gcd) {
return $b === 0 ? $a : $gcd($b, $a % $b);
};

$module[LOCATION_HANDLERS][HANDLER_QUESTION] = function () use ($gcd) {
$firstNum = random_int(1, 100);
$secondNum = random_int(1, 100);

$expectedAnswer = gmp_gcd($firstNum, $firstNum);
$expectedAnswer = $gcd($firstNum, $firstNum);
$question = "$firstNum $secondNum";
return [$question, $expectedAnswer];
};
Expand Down

0 comments on commit 541dcbb

Please sign in to comment.