Skip to content

Commit fc8b072

Browse files
Add perfect numbers exercise (#549)
Co-authored-by: Kah Goh <[email protected]>
1 parent 4227cb7 commit fc8b072

File tree

14 files changed

+235
-0
lines changed

14 files changed

+235
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,14 @@
614614
"practices": [],
615615
"prerequisites": [],
616616
"difficulty": 3
617+
},
618+
{
619+
"slug": "perfect-numbers",
620+
"name": "Perfect Numbers",
621+
"uuid": "d7d076c6-8ed4-4dcd-ae15-199dc4b28cf7",
622+
"practices": [],
623+
"prerequisites": [],
624+
"difficulty": 4
617625
}
618626
]
619627
},
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Instructions
2+
3+
Determine if a number is perfect, abundant, or deficient based on Nicomachus' (60 - 120 CE) classification scheme for positive integers.
4+
5+
The Greek mathematician [Nicomachus][nicomachus] devised a classification scheme for positive integers, identifying each as belonging uniquely to the categories of [perfect](#perfect), [abundant](#abundant), or [deficient](#deficient) based on their [aliquot sum][aliquot-sum].
6+
The _aliquot sum_ is defined as the sum of the factors of a number not including the number itself.
7+
For example, the aliquot sum of `15` is `1 + 3 + 5 = 9`.
8+
9+
## Perfect
10+
11+
A number is perfect when it equals its aliquot sum.
12+
For example:
13+
14+
- `6` is a perfect number because `1 + 2 + 3 = 6`
15+
- `28` is a perfect number because `1 + 2 + 4 + 7 + 14 = 28`
16+
17+
## Abundant
18+
19+
A number is abundant when it is less than its aliquot sum.
20+
For example:
21+
22+
- `12` is an abundant number because `1 + 2 + 3 + 4 + 6 = 16`
23+
- `24` is an abundant number because `1 + 2 + 3 + 4 + 6 + 8 + 12 = 36`
24+
25+
## Deficient
26+
27+
A number is deficient when it is greater than its aliquot sum.
28+
For example:
29+
30+
- `8` is a deficient number because `1 + 2 + 4 = 7`
31+
- Prime numbers are deficient
32+
33+
## Task
34+
35+
Implement a way to determine whether a given number is [perfect](#perfect).
36+
Depending on your language track, you may also need to implement a way to determine whether a given number is [abundant](#abundant) or [deficient](#deficient).
37+
38+
[nicomachus]: https://en.wikipedia.org/wiki/Nicomachus
39+
[aliquot-sum]: https://en.wikipedia.org/wiki/Aliquot_sum
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"authors": ["therealowenrees"],
3+
"files": {
4+
"solution": [
5+
"perfect_numbers.ml"
6+
],
7+
"test": [
8+
"test.ml"
9+
],
10+
"example": [
11+
".meta/example.ml"
12+
],
13+
"editor": [
14+
"perfect_numbers.mli"
15+
]
16+
},
17+
"blurb": "Determine if a number is perfect, abundant, or deficient based on Nicomachus' (60 - 120 CE) classification scheme for positive integers.",
18+
"source": "Taken from Chapter 2 of Functional Thinking by Neal Ford.",
19+
"source_url": "https://www.oreilly.com/library/view/functional-thinking/9781449365509/"
20+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
let classify n =
2+
let aliquot = function
3+
| 1 -> 0
4+
| n when n > 1 ->
5+
let rec sum_factors acc factor =
6+
if factor > n / 2 then acc
7+
else if n mod factor = 0 then
8+
sum_factors (acc + factor) (factor + 1)
9+
else
10+
sum_factors acc (factor + 1)
11+
in
12+
sum_factors 0 1
13+
| _ -> 0
14+
in
15+
match n with
16+
| _ when n < 1 -> Error "Classification is only possible for positive integers."
17+
| n ->
18+
let aliquot_sum = aliquot n in
19+
if aliquot_sum = n then Ok "perfect"
20+
else if aliquot_sum > n then Ok "abundant"
21+
else Ok "deficient"
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[163e8e86-7bfd-4ee2-bd68-d083dc3381a3]
13+
description = "Perfect numbers -> Smallest perfect number is classified correctly"
14+
15+
[169a7854-0431-4ae0-9815-c3b6d967436d]
16+
description = "Perfect numbers -> Medium perfect number is classified correctly"
17+
18+
[ee3627c4-7b36-4245-ba7c-8727d585f402]
19+
description = "Perfect numbers -> Large perfect number is classified correctly"
20+
21+
[80ef7cf8-9ea8-49b9-8b2d-d9cb3db3ed7e]
22+
description = "Abundant numbers -> Smallest abundant number is classified correctly"
23+
24+
[3e300e0d-1a12-4f11-8c48-d1027165ab60]
25+
description = "Abundant numbers -> Medium abundant number is classified correctly"
26+
27+
[ec7792e6-8786-449c-b005-ce6dd89a772b]
28+
description = "Abundant numbers -> Large abundant number is classified correctly"
29+
30+
[e610fdc7-2b6e-43c3-a51c-b70fb37413ba]
31+
description = "Deficient numbers -> Smallest prime deficient number is classified correctly"
32+
33+
[0beb7f66-753a-443f-8075-ad7fbd9018f3]
34+
description = "Deficient numbers -> Smallest non-prime deficient number is classified correctly"
35+
36+
[1c802e45-b4c6-4962-93d7-1cad245821ef]
37+
description = "Deficient numbers -> Medium deficient number is classified correctly"
38+
39+
[47dd569f-9e5a-4a11-9a47-a4e91c8c28aa]
40+
description = "Deficient numbers -> Large deficient number is classified correctly"
41+
42+
[a696dec8-6147-4d68-afad-d38de5476a56]
43+
description = "Deficient numbers -> Edge case (no factors other than itself) is classified correctly"
44+
45+
[72445cee-660c-4d75-8506-6c40089dc302]
46+
description = "Invalid inputs -> Zero is rejected (as it is not a positive integer)"
47+
48+
[2d72ce2c-6802-49ac-8ece-c790ba3dae13]
49+
description = "Invalid inputs -> Negative integer is rejected (as it is not a positive integer)"
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
default: clean test
2+
3+
test:
4+
dune runtest
5+
6+
clean:
7+
dune clean
8+
9+
.PHONY: clean
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
(executable
2+
(name test)
3+
(libraries base ounit2))
4+
5+
(alias
6+
(name runtest)
7+
(deps (:x test.exe))
8+
(action (run %{x})))
9+
10+
(alias
11+
(name buildtest)
12+
(deps (:x test.exe)))
13+
14+
(env
15+
(dev
16+
(flags (:standard -warn-error -A))))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(lang dune 1.1)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
let classify _ =
2+
failwith "'classify' is missing"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
val classify : int -> (string, string) result

0 commit comments

Comments
 (0)