Skip to content

Instantly share code, notes, and snippets.

@JeffreyWay
Last active January 17, 2020 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JeffreyWay/e66f646c7cdc606fb51fca93cd6bf02c to your computer and use it in GitHub Desktop.
Save JeffreyWay/e66f646c7cdc606fb51fca93cd6bf02c to your computer and use it in GitHub Desktop.
<?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