File tree Expand file tree Collapse file tree 4 files changed +48
-2
lines changed Expand file tree Collapse file tree 4 files changed +48
-2
lines changed Original file line number Diff line number Diff line change @@ -14,3 +14,5 @@ brain-gcd:
1414 ./bin/brain-gcd
1515brain-progression :
1616 ./bin/brain-progression
17+ brain-prime :
18+ ./bin/brain-prime
Original file line number Diff line number Diff line change 1+ #!/usr/bin/env php
2+
3+ <?php
4+
5+ require __DIR__ ."/../vendor/autoload.php " ;
6+
7+ \BrainGames \PrimeGame \startPrimeGame ();
Original file line number Diff line number Diff line change 99 " src/EvenGame.php" ,
1010 " src/CalcGame.php" ,
1111 " src/GcdGame.php" ,
12- " src/ProgressionGame.php"
12+ " src/ProgressionGame.php" ,
13+ " src/PrimeGame.php"
1314 ]
1415 },
1516 "bin" : [
1617 " bin/brain-games" ,
1718 " bin/brain-even" ,
1819 " bin/brain-calc" ,
1920 " bin/brain-gcd" ,
20- " bin/brain-progression"
21+ " bin/brain-progression" ,
22+ " bin/brain-prime"
2123 ],
2224 "authors" : [
2325 {
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace BrainGames \PrimeGame ;
4+
5+ use function BrainGames \Cli \playGame ;
6+
7+ function startPrimeGame (): void
8+ {
9+ $ questions = [];
10+ $ answers = [];
11+ for ($ i = 0 ; $ i < 3 ; $ i ++) {
12+ $ questions [$ i ] = rand (1 , 100 );
13+ $ answers [$ i ] = isPrime ($ questions [$ i ]) ? 'yes ' : 'no ' ;
14+ }
15+ playGame ($ questions , $ answers , 'Answer "yes" if given number is prime. Otherwise answer "no". ' );
16+ }
17+
18+ function isPrime (int $ number ): bool
19+ {
20+ if ($ number < 2 ) {
21+ return false ;
22+ }
23+ if ($ number === 2 ) {
24+ return true ;
25+ }
26+ if ($ number % 2 === 0 ) {
27+ return false ;
28+ }
29+ for ($ i = 3 ; $ i < sqrt ($ number ); $ i += 2 ) {
30+ if ($ number % $ i === 0 ) {
31+ return false ;
32+ }
33+ }
34+ return true ;
35+ }
You can’t perform that action at this time.
0 commit comments