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
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,14 @@
"practices": [],
"prerequisites": [],
"difficulty": 3
},
{
"slug": "perfect-numbers",
"name": "Perfect Numbers",
"uuid": "d7d076c6-8ed4-4dcd-ae15-199dc4b28cf7",
"practices": [],
"prerequisites": [],
"difficulty": 4
}
]
},
Expand Down
39 changes: 39 additions & 0 deletions exercises/practice/perfect-numbers/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# 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](#perfect), [abundant](#abundant), or [deficient](#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

A number is perfect when it equals its aliquot sum.
For example:

- `6` is a perfect number because `1 + 2 + 3 = 6`
- `28` is a perfect number because `1 + 2 + 4 + 7 + 14 = 28`

## Abundant

A number is abundant when it is less than its aliquot sum.
For example:

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

A number is deficient when it is greater than its aliquot sum.
For example:

- `8` is a deficient number because `1 + 2 + 4 = 7`
- Prime numbers are deficient

## Task

Implement a way to determine whether a given number is [perfect](#perfect).
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).

[nicomachus]: https://en.wikipedia.org/wiki/Nicomachus
[aliquot-sum]: https://en.wikipedia.org/wiki/Aliquot_sum
20 changes: 20 additions & 0 deletions exercises/practice/perfect-numbers/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"authors": ["therealowenrees"],
"files": {
"solution": [
"perfect_numbers.ml"
],
"test": [
"test.ml"
],
"example": [
".meta/example.ml"
],
"editor": [
"perfect_numbers.mli"
]
},
"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/"
}
21 changes: 21 additions & 0 deletions exercises/practice/perfect-numbers/.meta/example.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
let classify n =
let aliquot = function
| 1 -> 0
| n when n > 1 ->
let rec sum_factors acc factor =
if factor > n / 2 then acc
else if n mod factor = 0 then
sum_factors (acc + factor) (factor + 1)
else
sum_factors acc (factor + 1)
in
sum_factors 0 1
| _ -> 0
in
match n with
| _ when n < 1 -> Error "Classification is only possible for positive integers."
| n ->
let aliquot_sum = aliquot n in
if aliquot_sum = n then Ok "perfect"
else if aliquot_sum > n then Ok "abundant"
else Ok "deficient"
49 changes: 49 additions & 0 deletions exercises/practice/perfect-numbers/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# 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.

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

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

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

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

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

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

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

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

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

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

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

[72445cee-660c-4d75-8506-6c40089dc302]
description = "Invalid inputs -> Zero is rejected (as it is not a positive integer)"

[2d72ce2c-6802-49ac-8ece-c790ba3dae13]
description = "Invalid inputs -> Negative integer is rejected (as it is not a positive integer)"
9 changes: 9 additions & 0 deletions exercises/practice/perfect-numbers/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
default: clean test

test:
dune runtest

clean:
dune clean

.PHONY: clean
16 changes: 16 additions & 0 deletions exercises/practice/perfect-numbers/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
(executable
(name test)
(libraries base ounit2))

(alias
(name runtest)
(deps (:x test.exe))
(action (run %{x})))

(alias
(name buildtest)
(deps (:x test.exe)))

(env
(dev
(flags (:standard -warn-error -A))))
1 change: 1 addition & 0 deletions exercises/practice/perfect-numbers/dune-project
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(lang dune 1.1)
2 changes: 2 additions & 0 deletions exercises/practice/perfect-numbers/perfect_numbers.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
let classify _ =
failwith "'classify' is missing"
1 change: 1 addition & 0 deletions exercises/practice/perfect-numbers/perfect_numbers.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
val classify : int -> (string, string) result
40 changes: 40 additions & 0 deletions exercises/practice/perfect-numbers/test.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
open OUnit2
open Perfect_numbers

let option_printer = function
| Error m -> Printf.sprintf "Error \"%s\"" m
| Ok m -> Printf.sprintf "Ok \"%s\"" m

let ae exp got _test_ctxt = assert_equal ~printer:option_printer exp got

let tests = [
"Smallest perfect number is classified correctly" >::
ae (Ok "perfect") (classify (6));
"Medium perfect number is classified correctly" >::
ae (Ok "perfect") (classify (28));
"Large perfect number is classified correctly" >::
ae (Ok "perfect") (classify (33550336));
"Smallest abundant number is classified correctly" >::
ae (Ok "abundant") (classify (12));
"Medium abundant number is classified correctly" >::
ae (Ok "abundant") (classify (30));
"Large abundant number is classified correctly" >::
ae (Ok "abundant") (classify (33550335));
"Smallest prime deficient number is classified correctly" >::
ae (Ok "deficient") (classify (2));
"Smallest non-prime deficient number is classified correctly" >::
ae (Ok "deficient") (classify (4));
"Medium deficient number is classified correctly" >::
ae (Ok "deficient") (classify (32));
"Large deficient number is classified correctly" >::
ae (Ok "deficient") (classify (33550337));
"Edge case (no factors other than itself) is classified correctly" >::
ae (Ok "deficient") (classify (1));
"Zero is rejected (as it is not a positive integer)" >::
ae (Error "Classification is only possible for positive integers.") (classify (0));
"Negative integer is rejected (as it is not a positive integer)" >::
ae (Error "Classification is only possible for positive integers.") (classify (-1));
]

let () =
run_test_tt_main ("perfect-numbers tests" >::: tests)
1 change: 1 addition & 0 deletions templates/perfect-numbers/dune-project.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(lang dune 1.1)
20 changes: 20 additions & 0 deletions templates/perfect-numbers/test.ml.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
open OUnit2
open Perfect_numbers

let option_printer = function
| Error m -> Printf.sprintf "Error \"%s\"" m
| Ok m -> Printf.sprintf "Ok \"%s\"" m

let ae exp got _test_ctxt = assert_equal ~printer:option_printer exp got

let tests = [
{{#cases}}
{{#cases}}
"{{description}}" >::
ae {{#input}}{{{expected}}} (classify ({{number}})){{/input}};
{{/cases}}
{{/cases}}
]

let () =
run_test_tt_main ("perfect-numbers tests" >::: tests)
8 changes: 8 additions & 0 deletions test-generator/lib_generator/special_cases.ml
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,13 @@ let edit_collatz_conjecture_expected = function
| `Assoc [("error", `String m)] -> "(Error \"" ^ m ^ "\")"
| x -> Yojson.Basic.to_string x

let edit_perfect_numbers (ps: (string * json) list): (string * string) list =
let edit = function
| ("expected", `Assoc [("error", v)]) -> ("expected", "(Error " ^ json_to_string v ^ ")")
| ("expected", `String s) -> ("expected", "(Ok \"" ^ s ^ "\")")
| (k, v) -> (k, json_to_string v) in
List.map ps ~f:edit

let unwrap_strings (ps: (string * json) list): (string * string) list option =
let edit = function
| (_, `String s) -> ("params", s)
Expand All @@ -348,6 +355,7 @@ let edit_parameters ~(slug: string) (parameters: (string * json) list) = match (
| ("knapsack", ps) -> edit_knapsack ps |> Option.return
| ("minesweeper", ps) -> edit_minesweeper ps |> Option.return
| ("palindrome-products", ps) -> edit_palindrome_products ps |> Option.return
| ("perfect-numbers", ps) -> edit_perfect_numbers ps |> Option.return
| ("phone-number", ps) -> edit_expected ~f:edit_phone_number_expected ps |> Option.return
| ("rectangles", ps) -> edit_rectangles ps |> Option.return
| ("say", ps) -> edit_say ps |> Option.return
Expand Down