Add perfect-numbers exercise (#154)
Co-authored-by: Glenn Jackman <glenn.jackman@gmail.com>
This commit is contained in:
@@ -325,6 +325,14 @@
|
||||
"prerequisites": [],
|
||||
"difficulty": 4
|
||||
},
|
||||
{
|
||||
"slug": "perfect-numbers",
|
||||
"name": "Perfect Numbers",
|
||||
"uuid": "27acb535-fcbd-4b41-b2c9-f065c1441e45",
|
||||
"practices": [],
|
||||
"prerequisites": [],
|
||||
"difficulty": 4
|
||||
},
|
||||
{
|
||||
"slug": "rotational-cipher",
|
||||
"name": "Rotational Cipher",
|
||||
|
||||
39
exercises/practice/perfect-numbers/.docs/instructions.md
Normal file
39
exercises/practice/perfect-numbers/.docs/instructions.md
Normal file
@@ -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
|
||||
19
exercises/practice/perfect-numbers/.meta/config.json
Normal file
19
exercises/practice/perfect-numbers/.meta/config.json
Normal file
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"authors": [
|
||||
"erikschierboom"
|
||||
],
|
||||
"files": {
|
||||
"solution": [
|
||||
"perfect-numbers.8th"
|
||||
],
|
||||
"test": [
|
||||
"test.8th"
|
||||
],
|
||||
"example": [
|
||||
".meta/example.8th"
|
||||
]
|
||||
},
|
||||
"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/"
|
||||
}
|
||||
11
exercises/practice/perfect-numbers/.meta/example.8th
Normal file
11
exercises/practice/perfect-numbers/.meta/example.8th
Normal file
@@ -0,0 +1,11 @@
|
||||
: sum-of-factors \ n -- n
|
||||
dup >r 0 swap ( dup r@ swap n:mod !if n:+ else drop then ) 1 rot 2 n:/ loop
|
||||
;
|
||||
|
||||
: classify \ n -- s
|
||||
dup 1 n:< if drop null ;then
|
||||
dup sum-of-factors n:cmp
|
||||
dup 0 n:< if drop "abundant" ;then
|
||||
dup 0 n:> if drop "deficient" ;then
|
||||
drop "perfect"
|
||||
;
|
||||
49
exercises/practice/perfect-numbers/.meta/tests.toml
Normal file
49
exercises/practice/perfect-numbers/.meta/tests.toml
Normal file
@@ -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)"
|
||||
162
exercises/practice/perfect-numbers/libs/exercism/test
Normal file
162
exercises/practice/perfect-numbers/libs/exercism/test
Normal file
@@ -0,0 +1,162 @@
|
||||
needs console/loaded
|
||||
|
||||
\ -----------------------------------------------------------------
|
||||
|
||||
ns: test
|
||||
|
||||
-1 var, test-count
|
||||
var tests-passed
|
||||
var tests-failed
|
||||
var tests-skipped
|
||||
true var, run-test
|
||||
|
||||
\ Some utility words
|
||||
|
||||
: array-insens-dot \ x -- \\ removes leading space on stringified arrays
|
||||
dup >kind ns:a n:=
|
||||
if
|
||||
>s s:trim
|
||||
then
|
||||
.
|
||||
;
|
||||
|
||||
: test-passed \ s x x -- \\ test name, expected value, actual value
|
||||
2drop
|
||||
1 tests-passed n:+!
|
||||
con:green con:onBlack . space " ... OK" . con:white con:onBlack cr
|
||||
;
|
||||
|
||||
: test-skipped \ s --
|
||||
1 tests-skipped n:+!
|
||||
con:cyan con:onBlack . space " ... SKIPPED" . con:white con:onBlack cr
|
||||
;
|
||||
|
||||
: test-failed \ s x x -- \\ test name, expected value, actual value
|
||||
1 tests-failed n:+!
|
||||
rot
|
||||
con:red con:onBlack . space " ... FAIL" . con:white con:onBlack cr
|
||||
" Actual: «" . array-insens-dot "»" . cr
|
||||
" Expected: «" . array-insens-dot "»" . cr cr
|
||||
;
|
||||
|
||||
: isword? \ x -- x f
|
||||
dup >kind ns:w n:=
|
||||
;
|
||||
|
||||
: run-test? \ -- T
|
||||
run-test @ if true else "RUN_ALL_TESTS" getenv n:>bool then
|
||||
;
|
||||
|
||||
\ Num passed + num skipped + num failed should == num tests
|
||||
: all-tests-run? \ -- T
|
||||
tests-passed @ tests-skipped @ tests-failed @ n:+ n:+
|
||||
test-count @ n:=
|
||||
;
|
||||
|
||||
\ returns true if x is a date, false otherwise
|
||||
: date? \ x -- x T
|
||||
dup >kind ns:d n:=
|
||||
;
|
||||
|
||||
\ adapted from 8th forum -- https://8th-dev.com/forum/index.php/topic,2745.0.html
|
||||
: eq? \ x x -- T
|
||||
\ are the items the same kind?
|
||||
2dup >kind swap >kind n:=
|
||||
!if 2drop false ;then
|
||||
|
||||
\ same kind: try different comparators
|
||||
number? if n:= ;then
|
||||
string? if s:= ;then
|
||||
array? if ' eq? a:= 2nip ;then
|
||||
map? if ' eq? m:= 2nip ;then
|
||||
date? if d:= ;then
|
||||
|
||||
\ otherwise fall back to 'lazy evaluation'
|
||||
l: =
|
||||
;
|
||||
|
||||
: check-depth \ ... n -- ...
|
||||
dup>r
|
||||
n:1+ depth n:=
|
||||
!if
|
||||
con:red con:onBlack
|
||||
"PANIC: expected stack depth to be " . r> . cr
|
||||
"Stack is:" . cr
|
||||
.s cr
|
||||
255 die
|
||||
then
|
||||
rdrop
|
||||
;
|
||||
|
||||
\ -----------------------------------------------------------------
|
||||
|
||||
\ status report at end of run
|
||||
( all-tests-run?
|
||||
!if con:red con:onBlack "... FAIL - not all tests completed" . con:white con:onBlack cr then
|
||||
) onexit
|
||||
|
||||
\ Print a summary of the tests run
|
||||
( con:white con:onBlack
|
||||
test-count @ . space "tests planned - " .
|
||||
tests-passed @ . space "passed - " .
|
||||
tests-skipped @ . space "skipped - " .
|
||||
tests-failed @ . space "failed" . cr
|
||||
) onexit
|
||||
|
||||
\ -----------------------------------------------------------------
|
||||
\ The public-facing words
|
||||
\ -----------------------------------------------------------------
|
||||
|
||||
: equal? \ s x w -- | s w x --
|
||||
run-test? !if 2drop test-skipped ;; then
|
||||
isword? !if swap then
|
||||
w:exec
|
||||
3 check-depth
|
||||
2dup \ so test-failed can show actual and expected
|
||||
eq? if test-passed else test-failed then
|
||||
;
|
||||
|
||||
: true? \ s w --
|
||||
run-test? !if drop test-skipped ;; then
|
||||
w:exec
|
||||
2 check-depth
|
||||
true swap dup \ so test-failed can show actual and expected
|
||||
if test-passed else test-failed then
|
||||
;
|
||||
|
||||
: false? \ s w --
|
||||
run-test? !if drop test-skipped ;; then
|
||||
w:exec
|
||||
2 check-depth
|
||||
false swap dup \ so test-failed can show actual and expected
|
||||
!if test-passed else test-failed then
|
||||
;
|
||||
|
||||
: null? \ s w --
|
||||
run-test? !if drop test-skipped ;; then
|
||||
w:exec
|
||||
2 check-depth
|
||||
null swap dup \ so test-failed can show actual and expected
|
||||
G:null? nip if test-passed else test-failed then
|
||||
;
|
||||
|
||||
: SKIP-REST-OF-TESTS false run-test ! ;
|
||||
|
||||
: tests \ n --
|
||||
test-count !
|
||||
;
|
||||
|
||||
\ Set the exit status:
|
||||
\ 0 = all OK
|
||||
\ 1 = not all tests were run (some error occurred)
|
||||
\ 2 = some tests failed
|
||||
: end-of-tests \ --
|
||||
all-tests-run?
|
||||
if
|
||||
tests-failed @ 0 n:= if 0 else 2 then
|
||||
else
|
||||
1
|
||||
then
|
||||
die
|
||||
;
|
||||
|
||||
3
exercises/practice/perfect-numbers/perfect-numbers.8th
Normal file
3
exercises/practice/perfect-numbers/perfect-numbers.8th
Normal file
@@ -0,0 +1,3 @@
|
||||
: classify \ n -- s
|
||||
|
||||
;
|
||||
72
exercises/practice/perfect-numbers/test.8th
Normal file
72
exercises/practice/perfect-numbers/test.8th
Normal file
@@ -0,0 +1,72 @@
|
||||
"perfect-numbers.8th" f:include
|
||||
needs exercism/test
|
||||
with: test
|
||||
13 tests
|
||||
|
||||
"Smallest perfect number is classified correctly"
|
||||
( 6 classify )
|
||||
"perfect"
|
||||
equal?
|
||||
|
||||
SKIP-REST-OF-TESTS
|
||||
|
||||
"Medium perfect number is classified correctly"
|
||||
( 28 classify )
|
||||
"perfect"
|
||||
equal?
|
||||
|
||||
"Large perfect number is classified correctly"
|
||||
( 33550336 classify )
|
||||
"perfect"
|
||||
equal?
|
||||
|
||||
"Smallest abundant number is classified correctly"
|
||||
( 12 classify )
|
||||
"abundant"
|
||||
equal?
|
||||
|
||||
"Medium abundant number is classified correctly"
|
||||
( 30 classify )
|
||||
"abundant"
|
||||
equal?
|
||||
|
||||
"Large abundant number is classified correctly"
|
||||
( 33550335 classify )
|
||||
"abundant"
|
||||
equal?
|
||||
|
||||
"Smallest prime deficient number is classified correctly"
|
||||
( 2 classify )
|
||||
"deficient"
|
||||
equal?
|
||||
|
||||
"Smallest non-prime deficient number is classified correctly"
|
||||
( 4 classify )
|
||||
"deficient"
|
||||
equal?
|
||||
|
||||
"Medium deficient number is classified correctly"
|
||||
( 32 classify )
|
||||
"deficient"
|
||||
equal?
|
||||
|
||||
"Large deficient number is classified correctly"
|
||||
( 33550337 classify )
|
||||
"deficient"
|
||||
equal?
|
||||
|
||||
"Edge case (no factors other than itself) is classified correctly"
|
||||
( 1 classify )
|
||||
"deficient"
|
||||
equal?
|
||||
|
||||
"Zero is rejected (as it is not a positive integer)"
|
||||
( 0 classify )
|
||||
null?
|
||||
|
||||
"Negative integer is rejected (as it is not a positive integer)"
|
||||
( -1 classify )
|
||||
null?
|
||||
|
||||
end-of-tests
|
||||
;with
|
||||
Reference in New Issue
Block a user