Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions exercises/practice/prime-factors/.docs/instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,27 @@ Note that 1 is not a prime number.

What are the prime factors of 60?

- Our first divisor is 2. 2 goes into 60, leaving 30.
- Our first divisor is 2.
2 goes into 60, leaving 30.
- 2 goes into 30, leaving 15.
- 2 doesn't go cleanly into 15. So let's move on to our next divisor, 3.
- 2 doesn't go cleanly into 15.
So let's move on to our next divisor, 3.
- 3 goes cleanly into 15, leaving 5.
- 3 does not go cleanly into 5. The next possible factor is 4.
- 4 does not go cleanly into 5. The next possible factor is 5.
- 3 does not go cleanly into 5.
The next possible factor is 4.
- 4 does not go cleanly into 5.
The next possible factor is 5.
- 5 does go cleanly into 5.
- We're left only with 1, so now, we're done.

Our successful divisors in that computation represent the list of prime
factors of 60: 2, 2, 3, and 5.
Our successful divisors in that computation represent the list of prime factors of 60: 2, 2, 3, and 5.

You can check this yourself:

- 2 * 2 * 3 * 5
- = 4 * 15
- = 60
- Success!
```text
2 * 2 * 3 * 5
= 4 * 15
= 60
```

Success!
5 changes: 3 additions & 2 deletions exercises/practice/prime-factors/.meta/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"kunicmarko20",
"kytrinyx",
"petemcfarlane",
"yisraeldov"
"yisraeldov",
"Narkunan"
],
"files": {
"solution": [
Expand All @@ -22,5 +23,5 @@
},
"blurb": "Compute the prime factors of a given natural number.",
"source": "The Prime Factors Kata by Uncle Bob",
"source_url": "https://butunclebob.com/ArticleS.UncleBob.ThePrimeFactorsKata"
"source_url": "https://web.archive.org/web/20221026171801/http://butunclebob.com/ArticleS.UncleBob.ThePrimeFactorsKata"
}
22 changes: 0 additions & 22 deletions exercises/practice/prime-factors/.meta/example.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,5 @@
<?php

/*
* By adding type hints and enabling strict type checking, code can become
* easier to read, self-documenting and reduce the number of potential bugs.
* By default, type declarations are non-strict, which means they will attempt
* to change the original type to match the type specified by the
* type-declaration.
*
* In other words, if you pass a string to a function requiring a float,
* it will attempt to convert the string value to a float.
*
* To enable strict mode, a single declare directive must be placed at the top
* of the file.
* This means that the strictness of typing is configured on a per-file basis.
* This directive not only affects the type declarations of parameters, but also
* a function's return type.
*
* For more info review the Concept on strict type checking in the PHP track
* <link>.
*
* To disable strict typing, comment out the directive below.
*/

declare(strict_types=1);

function factors($n)
Expand Down
28 changes: 25 additions & 3 deletions exercises/practice/prime-factors/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -1,19 +1,41 @@
# This is an auto-generated file. Regular comments will be removed when this
# file is regenerated. Regenerating will not touch any manually added keys,
# so comments can be added in a "comment" key.
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[924fc966-a8f5-4288-82f2-6b9224819ccd]
description = "no factors"

[17e30670-b105-4305-af53-ddde182cb6ad]
description = "prime number"

[238d57c8-4c12-42ef-af34-ae4929f94789]
description = "another prime number"

[f59b8350-a180-495a-8fb1-1712fbee1158]
description = "square of a prime"

[756949d3-3158-4e3d-91f2-c4f9f043ee70]
description = "product of first prime"

[bc8c113f-9580-4516-8669-c5fc29512ceb]
description = "cube of a prime"

[7d6a3300-a4cb-4065-bd33-0ced1de6cb44]
description = "product of second prime"

[073ac0b2-c915-4362-929d-fc45f7b9a9e4]
description = "product of third prime"

[6e0e4912-7fb6-47f3-a9ad-dbcd79340c75]
description = "product of first and second prime"

[00485cd3-a3fe-4fbe-a64a-a4308fc1f870]
description = "product of primes and non-primes"

Expand Down
104 changes: 78 additions & 26 deletions exercises/practice/prime-factors/PrimeFactorsTest.php
Original file line number Diff line number Diff line change
@@ -1,29 +1,8 @@
<?php

/*
* By adding type hints and enabling strict type checking, code can become
* easier to read, self-documenting and reduce the number of potential bugs.
* By default, type declarations are non-strict, which means they will attempt
* to change the original type to match the type specified by the
* type-declaration.
*
* In other words, if you pass a string to a function requiring a float,
* it will attempt to convert the string value to a float.
*
* To enable strict mode, a single declare directive must be placed at the top
* of the file.
* This means that the strictness of typing is configured on a per-file basis.
* This directive not only affects the type declarations of parameters, but also
* a function's return type.
*
* For more info review the Concept on strict type checking in the PHP track
* <link>.
*
* To disable strict typing, comment out the directive below.
*/

declare(strict_types=1);

use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;

class PrimeFactorsTest extends TestCase
Expand All @@ -33,37 +12,110 @@ public static function setUpBeforeClass(): void
require_once 'PrimeFactors.php';
}

/**
* uuid: 924fc966-a8f5-4288-82f2-6b9224819ccd
*/
#[TestDox('no factors')]
public function testNoFactors(): void
{
$this->assertSame([], factors(1));
}

/**
* uuid: 17e30670-b105-4305-af53-ddde182cb6ad
*/
#[TestDox('prime number')]
public function testOneFactor(): void
{
$this->assertSame([2], factors(2));
}

public function testSquareOfPrime(): void
/**
* uuid: 238d57c8-4c12-42ef-af34-ae4929f94789
*/
#[TestDox('another prime number')]
public function testAnotherPrimeNumber(): void
{
$this->assertSame([3], factors(3));
}

/**
* uuid: f59b8350-a180-495a-8fb1-1712fbee1158
*/
#[TestDox('square of a prime')]
public function testSquareOfAPrime(): void
{
$this->assertSame([3, 3], factors(9));
}

public function testCubeOfPrime(): void
/**
* uuid: 756949d3-3158-4e3d-91f2-c4f9f043ee70
*/
#[TestDox('product of first prime')]
public function testProductOfFirstPrime(): void
{
$this->assertSame([2, 2], factors(4));
}

/**
* uuid: bc8c113f-9580-4516-8669-c5fc29512ceb
*/
#[TestDox('cube of a prime')]
public function testCubeOfAPrime(): void
{
$this->assertSame([2, 2, 2], factors(8));
}

public function testProductOfPrimesAndNon(): void
/**
* uuid: 7d6a3300-a4cb-4065-bd33-0ced1de6cb44
*/
#[TestDox('product of second prime')]
public function testProductOfSecondPrime(): void
{
$this->assertSame([3, 3, 3], factors(27));
}

/**
* uuid: 073ac0b2-c915-4362-929d-fc45f7b9a9e4
*/
#[TestDox('product of third prime')]
public function testProductOfThirdPrime(): void
{
$this->assertSame([5, 5, 5, 5], factors(625));
}

/**
* uuid: 6e0e4912-7fb6-47f3-a9ad-dbcd79340c75
*/
#[TestDox('product of first and second prime')]
public function testProductOfFirstAndSecondPrime(): void
{
$this->assertEquals([2, 3], factors(6));
}

/**
* uuid: 00485cd3-a3fe-4fbe-a64a-a4308fc1f870
*/
#[TestDox('product of primes and non-primes')]
public function testProductOfPrimesAndNonPrimes(): void
{
$this->assertEquals([2, 2, 3], factors(12));
}

/**
* uuid: 02251d54-3ca1-4a9b-85e1-b38f4b0ccb91
*/
#[TestDox('product of primes')]
public function testProductOfPrimes(): void
{
$this->assertEquals([5, 17, 23, 461], factors(901255));
}

public function testFactorsIncludeLargePrime(): void
/**
* uuid: 070cf8dc-e202-4285-aa37-8d775c9cd473
*/
#[TestDox('factors include a large prime')]
public function testFactorsIncludeALargePrime(): void
{
$this->assertEquals([11, 9539, 894119], factors(93819012551));
}
Expand Down
Loading