Skip to content

Commit 4392ea2

Browse files
Merge pull request #123 from keiravillekode/perfect-numbers
Add perfect-numbers exercise
2 parents 6da0b2e + c9bfecc commit 4392ea2

File tree

13 files changed

+384
-0
lines changed

13 files changed

+384
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,14 @@
9595
"prerequisites": [],
9696
"difficulty": 1
9797
},
98+
{
99+
"slug": "perfect-numbers",
100+
"name": "Perfect Numbers",
101+
"uuid": "0978dd36-069b-4570-8436-a3005d8f5dae",
102+
"practices": [],
103+
"prerequisites": [],
104+
"difficulty": 4
105+
},
98106
{
99107
"slug": "resistor-color",
100108
"name": "Resistor Color",
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Instruction append
2+
3+
## Return Value
4+
5+
Return one of the following integers:
6+
7+
| Classification | Return |
8+
|----------------|--------|
9+
| non-positive | 0 |
10+
| deficient | 1 |
11+
| perfect | 2 |
12+
| abundant | 3 |
13+
14+
## Reserved Addresses
15+
16+
No linear memory is required for this exercise.
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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"root": true,
3+
"extends": "@exercism/eslint-config-javascript",
4+
"env": {
5+
"jest": true
6+
},
7+
"overrides": [
8+
{
9+
"files": [
10+
"*.spec.js"
11+
],
12+
"excludedFiles": [
13+
"custom.spec.js"
14+
],
15+
"extends": "@exercism/eslint-config-javascript/maintainers"
16+
}
17+
]
18+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"authors": [
3+
"keiravillekode"
4+
],
5+
"files": {
6+
"solution": [
7+
"perfect-numbers.wat"
8+
],
9+
"test": [
10+
"perfect-numbers.spec.js"
11+
],
12+
"example": [
13+
".meta/proof.ci.wat"
14+
],
15+
"invalidator": [
16+
"package.json"
17+
]
18+
},
19+
"blurb": "Determine if a number is perfect, abundant, or deficient based on Nicomachus' (60 - 120 CE) classification scheme for positive integers.",
20+
"source": "Taken from Chapter 2 of Functional Thinking by Neal Ford.",
21+
"source_url": "https://www.oreilly.com/library/view/functional-thinking/9781449365509/",
22+
"custom": {
23+
"version.tests.compatibility": "jest-27",
24+
"flag.tests.task-per-describe": false,
25+
"flag.tests.may-run-long": false,
26+
"flag.tests.includes-optional": false
27+
}
28+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
(module
2+
3+
;;
4+
;; Determine if a number is perfect
5+
;;
6+
;; @param {i32} number - The number to check
7+
;;
8+
;; @returns {i32} 0 if non-positive, 1 if deficient, 2 if perfect, 3 if abundant
9+
;;
10+
(func (export "classify") (param $number i32) (result i32)
11+
(local $remaining i32)
12+
(local $p i32)
13+
(local $step i32)
14+
(local $factors i32)
15+
(local $total i32)
16+
17+
(if (i32.le_s (local.get $number) (i32.const 1)) (then
18+
;; The number 1 is deficient, while negative numbers and zero are non-positive.
19+
(return (i32.eq (local.get $number) (i32.const 1)))
20+
))
21+
22+
(local.set $remaining (local.get $number))
23+
(local.set $p (i32.const 2))
24+
(local.set $step (i32.const 1))
25+
(local.set $factors (i32.const 1))
26+
27+
(loop $prime
28+
(if (i32.lt_u (local.get $remaining) (i32.mul (local.get $p) (local.get $p))) (then
29+
(local.set $p (local.get $remaining))
30+
))
31+
32+
(if (i32.eqz (i32.rem_u (local.get $remaining) (local.get $p))) (then
33+
(local.set $total (i32.add (local.get $p) (i32.const 1)))
34+
(local.set $remaining (i32.div_u (local.get $remaining) (local.get $p)))
35+
(loop $power
36+
(if (i32.eqz (i32.rem_u (local.get $remaining) (local.get $p))) (then
37+
(local.set $remaining (i32.div_u (local.get $remaining) (local.get $p)))
38+
(local.set $total (i32.add (i32.mul (local.get $total) (local.get $p)) (i32.const 1)))
39+
(br $power)
40+
))
41+
)
42+
(local.set $factors (i32.mul (local.get $factors) (local.get $total)))
43+
(if (i32.eq (local.get $remaining) (i32.const 1)) (then
44+
(local.set $factors (i32.sub (local.get $factors) (local.get $number)))
45+
(if (i32.lt_u (local.get $factors) (local.get $number)) (then
46+
(return (i32.const 1))
47+
) (else
48+
(return (i32.add (i32.const 2) (i32.gt_u (local.get $factors) (local.get $number))))
49+
))
50+
))
51+
))
52+
53+
(local.set $p (i32.add (local.get $p) (local.get $step)))
54+
(local.set $step (i32.const 2))
55+
(br $prime)
56+
)
57+
(unreachable)
58+
)
59+
)
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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
audit=false
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 Exercism
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export default {
2+
presets: ["@exercism/babel-preset-javascript"],
3+
plugins: [],
4+
};

0 commit comments

Comments
 (0)