-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #123 from keiravillekode/perfect-numbers
Add perfect-numbers exercise
- Loading branch information
Showing
13 changed files
with
384 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
exercises/practice/perfect-numbers/.docs/instructions.append.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Instruction append | ||
|
||
## Return Value | ||
|
||
Return one of the following integers: | ||
|
||
| Classification | Return | | ||
|----------------|--------| | ||
| non-positive | 0 | | ||
| deficient | 1 | | ||
| perfect | 2 | | ||
| abundant | 3 | | ||
|
||
## Reserved Addresses | ||
|
||
No linear memory is required for this exercise. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"root": true, | ||
"extends": "@exercism/eslint-config-javascript", | ||
"env": { | ||
"jest": true | ||
}, | ||
"overrides": [ | ||
{ | ||
"files": [ | ||
"*.spec.js" | ||
], | ||
"excludedFiles": [ | ||
"custom.spec.js" | ||
], | ||
"extends": "@exercism/eslint-config-javascript/maintainers" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"authors": [ | ||
"keiravillekode" | ||
], | ||
"files": { | ||
"solution": [ | ||
"perfect-numbers.wat" | ||
], | ||
"test": [ | ||
"perfect-numbers.spec.js" | ||
], | ||
"example": [ | ||
".meta/proof.ci.wat" | ||
], | ||
"invalidator": [ | ||
"package.json" | ||
] | ||
}, | ||
"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/", | ||
"custom": { | ||
"version.tests.compatibility": "jest-27", | ||
"flag.tests.task-per-describe": false, | ||
"flag.tests.may-run-long": false, | ||
"flag.tests.includes-optional": false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
(module | ||
|
||
;; | ||
;; Determine if a number is perfect | ||
;; | ||
;; @param {i32} number - The number to check | ||
;; | ||
;; @returns {i32} 0 if non-positive, 1 if deficient, 2 if perfect, 3 if abundant | ||
;; | ||
(func (export "classify") (param $number i32) (result i32) | ||
(local $remaining i32) | ||
(local $p i32) | ||
(local $step i32) | ||
(local $factors i32) | ||
(local $total i32) | ||
|
||
(if (i32.le_s (local.get $number) (i32.const 1)) (then | ||
;; The number 1 is deficient, while negative numbers and zero are non-positive. | ||
(return (i32.eq (local.get $number) (i32.const 1))) | ||
)) | ||
|
||
(local.set $remaining (local.get $number)) | ||
(local.set $p (i32.const 2)) | ||
(local.set $step (i32.const 1)) | ||
(local.set $factors (i32.const 1)) | ||
|
||
(loop $prime | ||
(if (i32.lt_u (local.get $remaining) (i32.mul (local.get $p) (local.get $p))) (then | ||
(local.set $p (local.get $remaining)) | ||
)) | ||
|
||
(if (i32.eqz (i32.rem_u (local.get $remaining) (local.get $p))) (then | ||
(local.set $total (i32.add (local.get $p) (i32.const 1))) | ||
(local.set $remaining (i32.div_u (local.get $remaining) (local.get $p))) | ||
(loop $power | ||
(if (i32.eqz (i32.rem_u (local.get $remaining) (local.get $p))) (then | ||
(local.set $remaining (i32.div_u (local.get $remaining) (local.get $p))) | ||
(local.set $total (i32.add (i32.mul (local.get $total) (local.get $p)) (i32.const 1))) | ||
(br $power) | ||
)) | ||
) | ||
(local.set $factors (i32.mul (local.get $factors) (local.get $total))) | ||
(if (i32.eq (local.get $remaining) (i32.const 1)) (then | ||
(local.set $factors (i32.sub (local.get $factors) (local.get $number))) | ||
(if (i32.lt_u (local.get $factors) (local.get $number)) (then | ||
(return (i32.const 1)) | ||
) (else | ||
(return (i32.add (i32.const 2) (i32.gt_u (local.get $factors) (local.get $number)))) | ||
)) | ||
)) | ||
)) | ||
|
||
(local.set $p (i32.add (local.get $p) (local.get $step))) | ||
(local.set $step (i32.const 2)) | ||
(br $prime) | ||
) | ||
(unreachable) | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
audit=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2021 Exercism | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export default { | ||
presets: ["@exercism/babel-preset-javascript"], | ||
plugins: [], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
"name": "@exercism/wasm-perfect-numbers", | ||
"description": "Exercism exercises in WebAssembly.", | ||
"type": "module", | ||
"private": true, | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/exercism/wasm", | ||
"directory": "exercises/practice/perfect-numbers" | ||
}, | ||
"jest": { | ||
"maxWorkers": 1 | ||
}, | ||
"devDependencies": { | ||
"@babel/core": "^7.23.3", | ||
"@exercism/babel-preset-javascript": "^0.4.0", | ||
"@exercism/eslint-config-javascript": "^0.6.0", | ||
"@types/jest": "^29.5.8", | ||
"@types/node": "^20.9.1", | ||
"babel-jest": "^29.7.0", | ||
"core-js": "^3.33.2", | ||
"eslint": "^8.54.0", | ||
"jest": "^29.7.0" | ||
}, | ||
"dependencies": { | ||
"@exercism/wasm-lib": "^0.2.0" | ||
}, | ||
"scripts": { | ||
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js ./*", | ||
"watch": "node --experimental-vm-modules node_modules/jest/bin/jest.js --watch ./*", | ||
"lint": "eslint ." | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
exercises/practice/perfect-numbers/perfect-numbers.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { compileWat, WasmRunner } from "@exercism/wasm-lib"; | ||
|
||
let wasmModule; | ||
let currentInstance; | ||
|
||
function classify(number) { | ||
return currentInstance.exports.classify( | ||
number | ||
); | ||
} | ||
|
||
beforeAll(async () => { | ||
try { | ||
const watPath = new URL("./perfect-numbers.wat", import.meta.url); | ||
const { buffer } = await compileWat(watPath); | ||
wasmModule = await WebAssembly.compile(buffer); | ||
} catch (err) { | ||
console.log(`Error compiling *.wat: \n${err}`); | ||
process.exit(1); | ||
} | ||
}); | ||
|
||
describe("classify()", () => { | ||
beforeEach(async () => { | ||
currentInstance = null; | ||
|
||
if (!wasmModule) { | ||
return Promise.reject(); | ||
} | ||
try { | ||
currentInstance = await new WasmRunner(wasmModule); | ||
return Promise.resolve(); | ||
} catch (err) { | ||
console.log(`Error instantiating WebAssembly module: ${err}`); | ||
return Promise.reject(); | ||
} | ||
}); | ||
|
||
test("Smallest perfect number is classified correctly", () => { | ||
expect(classify(6)).toBe(2); | ||
}); | ||
|
||
xtest("Medium perfect number is classified correctly", () => { | ||
expect(classify(28)).toBe(2); | ||
}); | ||
|
||
xtest("Large perfect number is classified correctly", () => { | ||
expect(classify(33550336)).toBe(2); | ||
}); | ||
|
||
xtest("Smallest abundant number is classified correctly", () => { | ||
expect(classify(12)).toBe(3); | ||
}); | ||
|
||
xtest("Medium abundant number is classified correctly", () => { | ||
expect(classify(30)).toBe(3); | ||
}); | ||
|
||
xtest("Large abundant number is classified correctly", () => { | ||
expect(classify(33550335)).toBe(3); | ||
}); | ||
|
||
xtest("Smallest prime deficient number is classified correctly", () => { | ||
expect(classify(2)).toBe(1); | ||
}); | ||
|
||
xtest("Smallest non-prime deficient number is classified correctly", () => { | ||
expect(classify(4)).toBe(1); | ||
}); | ||
|
||
xtest("Medium deficient number is classified correctly", () => { | ||
expect(classify(32)).toBe(1); | ||
}); | ||
|
||
xtest("Large deficient number is classified correctly", () => { | ||
expect(classify(33550337)).toBe(1); | ||
}); | ||
|
||
xtest("Edge case (no factors other than itself) is classified correctly", () => { | ||
expect(classify(1)).toBe(1); | ||
}); | ||
|
||
xtest("Zero is rejected (as it is not a positive integer)", () => { | ||
expect(classify(0)).toBe(0); | ||
}); | ||
|
||
xtest("Negative integer is rejected (as it is not a positive integer)", () => { | ||
expect(classify(-1)).toBe(0); | ||
}); | ||
}); |
Oops, something went wrong.