Skip to content
This repository has been archived by the owner on May 30, 2023. It is now read-only.

Commit

Permalink
Add perfect-numbers exercise (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSchierboom authored May 7, 2023
1 parent 41e1cda commit fff71b7
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 0 deletions.
10 changes: 10 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,16 @@
],
"difficulty": 2
},
{
"slug": "perfect-numbers",
"name": "Perfect Numbers",
"uuid": "e523fad6-9078-46d0-a540-e6af83dc0e59",
"practices": [],
"prerequisites": [
"numbers"
],
"difficulty": 3
},
{
"slug": "pangram",
"name": "Pangram",
Expand Down
24 changes: 24 additions & 0 deletions exercises/practice/perfect-numbers/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Instructions

Determine if a number is perfect, abundant, or deficient based on
Nicomachus' (60 - 120 CE) classification scheme for positive integers.

The Greek mathematician [Nicomachus][nicomachus] devised a classification scheme for positive integers, identifying each as belonging uniquely to the categories of **perfect**, **abundant**, or **deficient** based on their [aliquot sum][aliquot-sum].
The aliquot sum is defined as the sum of the factors of a number not including the number itself.
For example, the aliquot sum of 15 is (1 + 3 + 5) = 9

- **Perfect**: aliquot sum = number
- 6 is a perfect number because (1 + 2 + 3) = 6
- 28 is a perfect number because (1 + 2 + 4 + 7 + 14) = 28
- **Abundant**: aliquot sum > number
- 12 is an abundant number because (1 + 2 + 3 + 4 + 6) = 16
- 24 is an abundant number because (1 + 2 + 3 + 4 + 6 + 8 + 12) = 36
- **Deficient**: aliquot sum < number
- 8 is a deficient number because (1 + 2 + 4) = 7
- Prime numbers are deficient

Implement a way to determine whether a given number is **perfect**.
Depending on your language track, you may also need to implement a way to determine whether a given number is **abundant** or **deficient**.

[nicomachus]: https://en.wikipedia.org/wiki/Nicomachus
[aliquot-sum]: https://en.wikipedia.org/wiki/Aliquot_sum
19 changes: 19 additions & 0 deletions exercises/practice/perfect-numbers/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"ErikSchierboom"
],
"files": {
"solution": [
"src/perfect_numbers.cljs"
],
"test": [
"test/perfect_numbers_test.cljs"
],
"example": [
".meta/src/example.cljs"
]
},
"blurb": "Determine if a number is perfect, abundant, or deficient based on Nicomachus' (60 - 120 CE) classification scheme for positive integers.",
"source": "Taken from Chapter 2 of Functional Thinking by Neal Ford.",
"source_url": "https://www.oreilly.com/library/view/functional-thinking/9781449365509/"
}
17 changes: 17 additions & 0 deletions exercises/practice/perfect-numbers/.meta/src/example.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
(ns perfect-numbers)

(defn- get-divisors
"Utility function to get the divisors of a number"
[number]
(for [n (range 1 (inc (quot number 2))) :when (zero? (mod number n))]
n))

(defn classify [number]
"Classifies a positive integer as deficient, abundant or perfect"
(if-not (pos? number)
(throw (js/Error. "Only positive numbers can be classified."))
(let [divisor-sum (apply + (get-divisors number))]
(cond
(> divisor-sum number) :abundant
(< divisor-sum number) :deficient
(= divisor-sum number) :perfect))))
42 changes: 42 additions & 0 deletions exercises/practice/perfect-numbers/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# 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.

[163e8e86-7bfd-4ee2-bd68-d083dc3381a3]
description = "Smallest perfect number is classified correctly"

[169a7854-0431-4ae0-9815-c3b6d967436d]
description = "Medium perfect number is classified correctly"

[ee3627c4-7b36-4245-ba7c-8727d585f402]
description = "Large perfect number is classified correctly"

[80ef7cf8-9ea8-49b9-8b2d-d9cb3db3ed7e]
description = "Smallest abundant number is classified correctly"

[3e300e0d-1a12-4f11-8c48-d1027165ab60]
description = "Medium abundant number is classified correctly"

[ec7792e6-8786-449c-b005-ce6dd89a772b]
description = "Large abundant number is classified correctly"

[e610fdc7-2b6e-43c3-a51c-b70fb37413ba]
description = "Smallest prime deficient number is classified correctly"

[0beb7f66-753a-443f-8075-ad7fbd9018f3]
description = "Smallest non-prime deficient number is classified correctly"

[1c802e45-b4c6-4962-93d7-1cad245821ef]
description = "Medium deficient number is classified correctly"

[47dd569f-9e5a-4a11-9a47-a4e91c8c28aa]
description = "Large deficient number is classified correctly"

[a696dec8-6147-4d68-afad-d38de5476a56]
description = "Edge case (no factors other than itself) is classified correctly"

[72445cee-660c-4d75-8506-6c40089dc302]
description = "Zero is rejected (not a natural number)"

[2d72ce2c-6802-49ac-8ece-c790ba3dae13]
description = "Negative integer is rejected (not a natural number)"
10 changes: 10 additions & 0 deletions exercises/practice/perfect-numbers/deps.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{:deps
{org.clojure/clojure {:mvn/version "1.10.1"}
org.clojure/clojurescript {:mvn/version "1.10.773"}}

:aliases
{:test
{:extra-paths ["test"]
:extra-deps
{olical/cljs-test-runner {:mvn/version "3.8.0"}}
:main-opts ["-m" "cljs-test-runner.main"]}}}
5 changes: 5 additions & 0 deletions exercises/practice/perfect-numbers/src/perfect_numbers.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(ns perfect-numbers)

(defn classify [] ;; <- arglist goes here
;; your code goes here
)
20 changes: 20 additions & 0 deletions exercises/practice/perfect-numbers/test/perfect_numbers_test.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
(ns perfect-numbers-test
(:require
[clojure.test :refer [deftest testing is]]
[perfect-numbers]))

(deftest test-initialize-perfect-number
(testing "Negative numbers throw an exception"
(is (thrown? js/Error (perfect-numbers/classify -1)))))

(deftest test-classify-deficient
(testing "13 is a deficient number"
(is (= :deficient (perfect-numbers/classify 13)))))

(deftest test-classify-perfect
(testing "28 is a perfect number"
(is (= :perfect (perfect-numbers/classify 28)))))

(deftest test-classify-abundant
(testing "12 is an abundant number"
(is (= :abundant (perfect-numbers/classify 12)))))

0 comments on commit fff71b7

Please sign in to comment.