hallernsk/php-project-lvl1

View on GitHub
src/games/gcd.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php

namespace BrainGames\games\gcd;

use function BrainGames\games\runGame;

use const BrainGames\games\NUM_OF_ROUNDS;

function getGCD($a, $b)
{
    while ($a !== 0 && $b !== 0) {
        if ($a > $b) {
            $a =  $a % $b;
        } else {
            $b = $b % $a;
        }
    }
    return $a + $b;
}

function run()
{
    $task = "Find the greatest common divisor of given numbers.";
    $questionsAnswers = [];
    for ($i = 0; $i < NUM_OF_ROUNDS; $i++) {
        $num1 = mt_rand(1, 100);
        $num2 = mt_rand(1, 100);
        $question = "{$num1} {$num2}";
        $correctAnswer = (string) getGCD($num1, $num2);
        $questionsAnswers[$i] = [$question, $correctAnswer];
    }
    runGame($task, $questionsAnswers);
}