Skip to content

Commit

Permalink
Add prime-factors exercise (#372)
Browse files Browse the repository at this point in the history
  • Loading branch information
keiravillekode authored May 8, 2024
1 parent 379da66 commit 38895a4
Show file tree
Hide file tree
Showing 7 changed files with 204 additions and 0 deletions.
11 changes: 11 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,17 @@
"strings"
]
},
{
"slug": "prime-factors",
"name": "Prime Factors",
"uuid": "cb79cb42-7efe-49fe-98c4-68dbdc32f2a3",
"practices": [],
"prerequisites": [],
"difficulty": 3,
"topics": [
"math"
]
},
{
"slug": "largest-series-product",
"name": "Largest Series Product",
Expand Down
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.el"
],
"test": [
"prime-factors-test.el"
],
"example": [
".meta/example.el"
]
},
"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"
}
19 changes: 19 additions & 0 deletions exercises/practice/prime-factors/.meta/example.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
;;; prime-factors.el --- Prime Factors (exercism) -*- lexical-binding: t; -*-

;;; Commentary:

;;; Code:

(require 'cl-lib)

(defun factors-from (start value)
(unless (= value 1)
(cl-loop for p from start
until (= 0 (% value p))
finally return (cons p (factors-from p (/ value p))))))

(defun factors (value)
(factors-from 2 value))

(provide 'prime-factors)
;;; prime-factors.el ends here
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"
60 changes: 60 additions & 0 deletions exercises/practice/prime-factors/prime-factors-test.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
;;; prime-factors-test.el --- Tests for Prime Factors (exercism) -*- lexical-binding: t; -*-

;;; Commentary:

;;; Code:

(load-file "prime-factors.el")
(declare-function factors "prime-factors.el" (value))


(ert-deftest no-factors ()
(should (equal '() (factors 1))))


(ert-deftest prime-number ()
(should (equal '(2) (factors 2))))


(ert-deftest another-prime-number ()
(should (equal '(3) (factors 3))))


(ert-deftest square-of-a-prime ()
(should (equal '(3 3) (factors 9))))


(ert-deftest product-of-first-prime ()
(should (equal '(2 2) (factors 4))))


(ert-deftest cube-of-a-prime ()
(should (equal '(2 2 2) (factors 8))))


(ert-deftest product-of-second-prime ()
(should (equal '(3 3 3) (factors 27))))


(ert-deftest product-of-third-prime ()
(should (equal '(5 5 5 5) (factors 625))))


(ert-deftest product-of-first-and-second-prime ()
(should (equal '(2 3) (factors 6))))


(ert-deftest product-of-primes-and-non-primes ()
(should (equal '(2 2 3) (factors 12))))


(ert-deftest product-of-primes ()
(should (equal '(5 17 23 461) (factors 901255))))


(ert-deftest factors-include-a-large-prime ()
(should (equal '(11 9539 894119) (factors 93819012551))))


(provide 'prime-factors-test)
;;; prime-factors-test.el ends here
13 changes: 13 additions & 0 deletions exercises/practice/prime-factors/prime-factors.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
;;; prime-factors.el --- Prime Factors (exercism) -*- lexical-binding: t; -*-

;;; Commentary:

;;; Code:


(defun factors (value)
(error "Delete this S-Expression and write your own implementation"))


(provide 'prime-factors)
;;; prime-factors.el ends here

0 comments on commit 38895a4

Please sign in to comment.