Skip to content

Commit 5aadb15

Browse files
committed
Add perfect-numbers
1 parent 0176e6c commit 5aadb15

9 files changed

Lines changed: 220 additions & 0 deletions

File tree

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,14 @@
322322
"prerequisites": [],
323323
"difficulty": 3
324324
},
325+
{
326+
"slug": "perfect-numbers",
327+
"name": "Perfect Numbers",
328+
"uuid": "ad78ef27-70a9-4625-b432-742f75cae29c",
329+
"practices": [],
330+
"prerequisites": [],
331+
"difficulty": 3
332+
},
325333
{
326334
"slug": "phone-number",
327335
"name": "Phone Number",
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
return {
2+
default = {
3+
ROOT = { '.' }
4+
}
5+
}
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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"glennj"
4+
],
5+
"files": {
6+
"solution": [
7+
"perfect_numbers.moon"
8+
],
9+
"test": [
10+
"perfect_numbers_spec.moon"
11+
],
12+
"example": [
13+
".meta/example.moon"
14+
]
15+
},
16+
"blurb": "Determine if a number is perfect, abundant, or deficient based on Nicomachus' (60 - 120 CE) classification scheme for positive integers.",
17+
"source": "Taken from Chapter 2 of Functional Thinking by Neal Ford.",
18+
"source_url": "https://www.oreilly.com/library/view/functional-thinking/9781449365509/"
19+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import fold from require 'moon'
2+
3+
aliquot_sum = (number) ->
4+
factors = {}
5+
f = 1
6+
while f * f <= number
7+
g = number // f
8+
if g * f == number
9+
table.insert factors, f
10+
if g != f
11+
table.insert factors, g
12+
f += 1
13+
-- the sum of the factors not including the number
14+
fold(factors, (sum, num) -> sum + num) - number
15+
16+
{
17+
classify: (number) ->
18+
assert number > 0, 'Classification is only possible for positive integers.'
19+
20+
sum = aliquot_sum number
21+
if sum == number
22+
'perfect'
23+
elseif sum < number
24+
'deficient'
25+
else
26+
'abundant'
27+
}
28+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
module_imports: {'classify'},
3+
4+
generate_test: (case, level) ->
5+
lines = if case.expected.error
6+
{
7+
"func = -> #{case.property} #{case.input.number}",
8+
"assert.has.error func, #{quote case.expected.error}"
9+
}
10+
else
11+
{
12+
"assert.are.equal #{quote case.expected}, #{case.property} #{case.input.number}"
13+
}
14+
table.concat [indent line, level for line in *lines], '\n'
15+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
[05f15b93-849c-45e9-9c7d-1ea131ef7d10]
31+
description = "Abundant numbers -> Perfect square abundant number is classified correctly"
32+
33+
[e610fdc7-2b6e-43c3-a51c-b70fb37413ba]
34+
description = "Deficient numbers -> Smallest prime deficient number is classified correctly"
35+
36+
[0beb7f66-753a-443f-8075-ad7fbd9018f3]
37+
description = "Deficient numbers -> Smallest non-prime deficient number is classified correctly"
38+
39+
[1c802e45-b4c6-4962-93d7-1cad245821ef]
40+
description = "Deficient numbers -> Medium deficient number is classified correctly"
41+
42+
[47dd569f-9e5a-4a11-9a47-a4e91c8c28aa]
43+
description = "Deficient numbers -> Large deficient number is classified correctly"
44+
45+
[a696dec8-6147-4d68-afad-d38de5476a56]
46+
description = "Deficient numbers -> Edge case (no factors other than itself) is classified correctly"
47+
48+
[72445cee-660c-4d75-8506-6c40089dc302]
49+
description = "Invalid inputs -> Zero is rejected (as it is not a positive integer)"
50+
51+
[2d72ce2c-6802-49ac-8ece-c790ba3dae13]
52+
description = "Invalid inputs -> Negative integer is rejected (as it is not a positive integer)"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
classify: (number) ->
3+
error 'Implement me'
4+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import classify from require 'perfect_numbers'
2+
3+
describe 'perfect-numbers', ->
4+
describe 'Perfect numbers', ->
5+
it 'Smallest perfect number is classified correctly', ->
6+
assert.are.equal 'perfect', classify 6
7+
8+
pending 'Medium perfect number is classified correctly', ->
9+
assert.are.equal 'perfect', classify 28
10+
11+
pending 'Large perfect number is classified correctly', ->
12+
assert.are.equal 'perfect', classify 33550336
13+
14+
describe 'Abundant numbers', ->
15+
pending 'Smallest abundant number is classified correctly', ->
16+
assert.are.equal 'abundant', classify 12
17+
18+
pending 'Medium abundant number is classified correctly', ->
19+
assert.are.equal 'abundant', classify 30
20+
21+
pending 'Large abundant number is classified correctly', ->
22+
assert.are.equal 'abundant', classify 33550335
23+
24+
pending 'Perfect square abundant number is classified correctly', ->
25+
assert.are.equal 'abundant', classify 196
26+
27+
describe 'Deficient numbers', ->
28+
pending 'Smallest prime deficient number is classified correctly', ->
29+
assert.are.equal 'deficient', classify 2
30+
31+
pending 'Smallest non-prime deficient number is classified correctly', ->
32+
assert.are.equal 'deficient', classify 4
33+
34+
pending 'Medium deficient number is classified correctly', ->
35+
assert.are.equal 'deficient', classify 32
36+
37+
pending 'Large deficient number is classified correctly', ->
38+
assert.are.equal 'deficient', classify 33550337
39+
40+
pending 'Edge case (no factors other than itself) is classified correctly', ->
41+
assert.are.equal 'deficient', classify 1
42+
43+
describe 'Invalid inputs', ->
44+
pending 'Zero is rejected (as it is not a positive integer)', ->
45+
func = -> classify 0
46+
assert.has.error func, 'Classification is only possible for positive integers.'
47+
48+
pending 'Negative integer is rejected (as it is not a positive integer)', ->
49+
func = -> classify -1
50+
assert.has.error func, 'Classification is only possible for positive integers.'

0 commit comments

Comments
 (0)