Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add prime-factors exercise #215

Merged
merged 2 commits into from
Oct 27, 2024
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
478 changes: 243 additions & 235 deletions config.json

Large diffs are not rendered by default.

36 changes: 36 additions & 0 deletions exercises/practice/prime-factors/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Instructions

Compute the prime factors of a given natural number.

A prime number is only evenly divisible by itself and 1.

Note that 1 is not a prime number.

## Example

What are the prime factors of 60?

- 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.
- 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.
- 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.

You can check this yourself:

```text
2 * 2 * 3 * 5
= 4 * 15
= 60
```

Success!
19 changes: 19 additions & 0 deletions exercises/practice/prime-factors/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"keiravillekode"
],
"files": {
"solution": [
"prime-factors.wren"
],
"test": [
"prime-factors.spec.wren"
],
"example": [
".meta/proof.ci.wren"
]
},
"blurb": "Compute the prime factors of a given natural number.",
"source": "The Prime Factors Kata by Uncle Bob",
"source_url": "https://web.archive.org/web/20221026171801/http://butunclebob.com/ArticleS.UncleBob.ThePrimeFactorsKata"
}
15 changes: 15 additions & 0 deletions exercises/practice/prime-factors/.meta/proof.ci.wren
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class PrimeFactors {
static factors(value) {
var result = []
var factor = 2
while (value > 1) {
if (value % factor == 0) {
result.add(factor)
value = value / factor
} else {
factor = factor + 1
}
}
return result
}
}
46 changes: 46 additions & 0 deletions exercises/practice/prime-factors/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# 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"

[02251d54-3ca1-4a9b-85e1-b38f4b0ccb91]
description = "product of primes"

[070cf8dc-e202-4285-aa37-8d775c9cd473]
description = "factors include a large prime"
21 changes: 21 additions & 0 deletions exercises/practice/prime-factors/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Exercism

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
14 changes: 14 additions & 0 deletions exercises/practice/prime-factors/package.wren
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import "wren-package" for WrenPackage, Dependency
import "os" for Process

class Package is WrenPackage {
construct new() {}
name { "exercism/prime-factors" }
dependencies {
return [
Dependency.new("wren-testie", "0.3.0", "https://github.com/joshgoebel/wren-testie.git")
]
}
}

Package.new().default()
52 changes: 52 additions & 0 deletions exercises/practice/prime-factors/prime-factors.spec.wren
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import "./prime-factors" for PrimeFactors
import "wren-testie/testie" for Testie, Expect

Testie.test("PrimeFactors") { |do, skip|
do.test("no factors") {
Expect.value(PrimeFactors.factors(1)).toEqual([])
}

skip.test("prime number") {
Expect.value(PrimeFactors.factors(2)).toEqual([2])
}

skip.test("another prime number") {
Expect.value(PrimeFactors.factors(3)).toEqual([3])
}

skip.test("square of a prime") {
Expect.value(PrimeFactors.factors(9)).toEqual([3, 3])
}

skip.test("product of first prime") {
Expect.value(PrimeFactors.factors(4)).toEqual([2, 2])
}

skip.test("cube of a prime") {
Expect.value(PrimeFactors.factors(8)).toEqual([2, 2, 2])
}

skip.test("product of second prime") {
Expect.value(PrimeFactors.factors(27)).toEqual([3, 3, 3])
}

skip.test("product of third prime") {
Expect.value(PrimeFactors.factors(625)).toEqual([5, 5, 5, 5])
}

skip.test("product of first and second prime") {
Expect.value(PrimeFactors.factors(6)).toEqual([2, 3])
}

skip.test("product of primes and non-primes") {
Expect.value(PrimeFactors.factors(12)).toEqual([2, 2, 3])
}

skip.test("product of primes") {
Expect.value(PrimeFactors.factors(901255)).toEqual([5, 17, 23, 461])
}

skip.test("factors include a large prime") {
Expect.value(PrimeFactors.factors(93819012551)).toEqual([11, 9539, 894119])
}
}
5 changes: 5 additions & 0 deletions exercises/practice/prime-factors/prime-factors.wren
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class PrimeFactors {
static factors(value) {
Fiber.abort("Remove this statement and implement this function")
}
}