Last active
January 17, 2020 14:18
-
-
Save JeffreyWay/e66f646c7cdc606fb51fca93cd6bf02c to your computer and use it in GitHub Desktop.
Prime Factors Kata - https://laracasts.com/series/code-katas-with-phpunit/episodes/2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
class PrimeFactors | |
{ | |
public function generate($number) | |
{ | |
$factors = []; | |
for ($divisor = 2; $number > 1; $divisor++) { | |
for (; $number % $divisor === 0; $number /= $divisor) { | |
$factors[] = $divisor; | |
} | |
} | |
return $factors; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use App\PrimeFactors; | |
class PrimeFactorsTest extends \PHPUnit\Framework\TestCase | |
{ | |
/** | |
* @test | |
* @dataProvider factors | |
*/ | |
function it_generates_prime_factors($number, $expected) | |
{ | |
$factors = new PrimeFactors(); | |
$this->assertEquals($expected, $factors->generate($number)); | |
} | |
public function factors() | |
{ | |
return [ | |
[1, []], | |
[2, [2]], | |
[3, [3]], | |
[4, [2, 2]], | |
[5, [5]], | |
[6, [2, 3]], | |
[7, [7]], | |
[8, [2, 2, 2]], | |
[9, [3, 3]], | |
[11, [11]], | |
[12, [2, 2, 3]], | |
[17, [17]], | |
[100, [2, 2, 5, 5]] | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment