Skip to content

Commit c7939d6

Browse files
authored
Add perfect-numbers (#333)
1 parent 658a964 commit c7939d6

File tree

7 files changed

+186
-0
lines changed

7 files changed

+186
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,14 @@
335335
"math"
336336
]
337337
},
338+
{
339+
"slug": "perfect-numbers",
340+
"name": "Perfect Numbers",
341+
"uuid": "6f95df06-8171-44a6-8fb3-d43951b9fae3",
342+
"practices": [],
343+
"prerequisites": [],
344+
"difficulty": 2
345+
},
338346
{
339347
"slug": "phone-number",
340348
"name": "Phone Number",
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+
"BNAndras"
4+
],
5+
"files": {
6+
"solution": [
7+
"perfect-numbers.coffee"
8+
],
9+
"test": [
10+
"perfect-numbers.spec.coffee"
11+
],
12+
"example": [
13+
".meta/example.coffee"
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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class PerfectNumbers
2+
@classify: (number) ->
3+
if number <= 0
4+
throw new Error 'Classification is only possible for positive integers.'
5+
6+
sum = 0
7+
for i in [1...number]
8+
if number % i == 0
9+
sum += i
10+
11+
if sum < number
12+
'deficient'
13+
else if sum > number
14+
'abundant'
15+
else
16+
'perfect'
17+
18+
module.exports = PerfectNumbers
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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class PerfectNumbers
2+
@classify: (number) ->
3+
4+
module.exports = PerfectNumbers
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
PerfectNumbers = require './perfect-numbers'
2+
3+
describe 'Perfect Numbers', ->
4+
describe 'Perfect numbers', ->
5+
it 'Smallest perfect number is classified correctly', ->
6+
expect(PerfectNumbers.classify(6)).toEqual 'perfect'
7+
8+
xit 'Medium perfect number is classified correctly', ->
9+
expect(PerfectNumbers.classify(28)).toEqual 'perfect'
10+
11+
xit 'Large perfect number is classified correctly', ->
12+
expect(PerfectNumbers.classify(33550336)).toEqual 'perfect'
13+
14+
describe 'Abundant numbers', ->
15+
xit 'Smallest abundant number is classified correctly', ->
16+
expect(PerfectNumbers.classify(12)).toEqual 'abundant'
17+
18+
xit 'Medium abundant number is classified correctly', ->
19+
expect(PerfectNumbers.classify(30)).toEqual 'abundant'
20+
21+
xit 'Large abundant number is classified correctly', ->
22+
expect(PerfectNumbers.classify(33550335)).toEqual 'abundant'
23+
24+
describe 'Deficient numbers', ->
25+
xit 'Smallest prime deficient number is classified correctly', ->
26+
expect(PerfectNumbers.classify(2)).toEqual 'deficient'
27+
28+
xit 'Smallest non-prime deficient number is classified correctly', ->
29+
expect(PerfectNumbers.classify(4)).toEqual 'deficient'
30+
31+
xit 'Medium deficient number is classified correctly', ->
32+
expect(PerfectNumbers.classify(32)).toEqual 'deficient'
33+
34+
xit 'Large deficient number is classified correctly', ->
35+
expect(PerfectNumbers.classify(33550337)).toEqual 'deficient'
36+
37+
xit 'Edge case (no factors other than itself) is classified correctly', ->
38+
expect(PerfectNumbers.classify(1)).toEqual 'deficient'
39+
40+
describe 'Invalid inputs', ->
41+
xit 'Zero is rejected (as it is not a positive integer)', ->
42+
expect ->
43+
PerfectNumbers.classify(0)
44+
.toThrow new Error('Classification is only possible for positive integers.')
45+
46+
xit 'Negative integer is rejected (as it is not a positive integer)', ->
47+
expect ->
48+
PerfectNumbers.classify(-1)
49+
.toThrow new Error('Classification is only possible for positive integers.')

0 commit comments

Comments
 (0)