itohin/project-lvl1-s426

View on GitHub
src/Games/IsPrime.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php

namespace BrainGames\Games\IsPrime;

use function BrainGames\Kernel\game;

const TASK_OF_GAME = 'Answer "yes" if given number is prime. Otherwise answer "no".';

function run()
{
    $getGameData = function () {
        $question = rand(1, 50);
        $correct = isPrime($question) ? 'yes' : 'no';

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

function isPrime($number)
{
    if ($number < 2) {
        return false;
    }
    
    for ($i = 2; $i <= sqrt($number); $i++) {
        if ($number % $i == 0) {
            return false;
        }
    }
    return true;
}