Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?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;
}
}
<?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
You can’t perform that action at this time.