itohin/project-lvl1-s426

View on GitHub
src/Games/Gcd.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php

namespace BrainGames\Games\Gcd;

use function BrainGames\Kernel\game;

const TASK_OF_GAME = 'Find the greatest common divisor of given numbers.';

function run()
{
    $getGameData = function () {
        $firstNumber = rand(1, 99);
        $secondNumber = rand(1, 99);

        $question = "{$firstNumber} {$secondNumber}";
        $correct = gcd($firstNumber, $secondNumber);

        return [$question, $correct];
    };
    game(TASK_OF_GAME, $getGameData);
}

function gcd(int $first, int $second)
{
    if ($second == 0) {
        return $first;
    }
    return gcd($second, $first % $second);
}