exercism/julia/perfect-numbers/perfect-numbers.ipynb
2022-08-24 14:28:45 +02:00

69 lines
5.8 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": ["# Perfect Numbers\n", "\n", "Determine if a number is perfect, abundant, or deficient based on\n", "Nicomachus' (60 - 120 CE) classification scheme for natural numbers.\n", "\n", "The Greek mathematician [Nicomachus](https://en.wikipedia.org/wiki/Nicomachus) devised a classification scheme for natural numbers, identifying each as belonging uniquely to the categories of **perfect**, **abundant**, or **deficient** based on their [aliquot sum](https://en.wikipedia.org/wiki/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\n", "\n", "- **Perfect**: aliquot sum = number\n", " - 6 is a perfect number because (1 + 2 + 3) = 6\n", " - 28 is a perfect number because (1 + 2 + 4 + 7 + 14) = 28\n", "- **Abundant**: aliquot sum > number\n", " - 12 is an abundant number because (1 + 2 + 3 + 4 + 6) = 16\n", " - 24 is an abundant number because (1 + 2 + 3 + 4 + 6 + 8 + 12) = 36\n", "- **Deficient**: aliquot sum < number\n", " - 8 is a deficient number because (1 + 2 + 4) = 7\n", " - Prime numbers are deficient\n", "\n", "Implement a way to determine whether a given number is **perfect**, **abundant** or **deficient**.\n", "\n", "## Source\n", "\n", "Taken from Chapter 2 of Functional Thinking by Neal Ford. [http://shop.oreilly.com/product/0636920029687.do](http://shop.oreilly.com/product/0636920029687.do)\n", "\n", "## Version compatibility\n", "This exercise has been tested on Julia versions >=1.0.\n", "\n", "## Submitting Incomplete Solutions\n", "It's possible to submit an incomplete solution so you can see how others have completed the exercise."]
},
{
"cell_type": "markdown",
"metadata": {},
"source": ["## Your solution"]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": ["# submit\n", ""]
},
{
"cell_type": "markdown",
"metadata": {},
"source": ["## Test suite"]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": ["using Test\n", "\n", "# include(\"perfect-numbers.jl\")\n", "\n", "@testset \"Perfect numbers\" begin\n", "\n", " @testset \"Smallest perfect number is classified correctly\" begin\n", " @test isperfect(6)\n", " end\n", "\n", " @testset \"Medium perfect number is classified correctly\" begin\n", " @test isperfect(28)\n", " end\n", "\n", " @testset \"Large perfect number is classified correctly\" begin\n", " @test isperfect(33550336)\n", " end\n", "end\n", "\n", "@testset \"Abundant numbers\" begin\n", "\n", " @testset \"Smallest abundant number is classified correctly\" begin\n", " @test isabundant(12)\n", " end\n", "\n", " @testset \"Medium abundant number is classified correctly\" begin\n", " @test isabundant(30)\n", " end\n", "\n", " @testset \"Large abundant number is classified correctly\" begin\n", " @test isabundant(33550335)\n", " end\n", "end\n", "\n", "@testset \"Deficient numbers\" begin\n", "\n", " @testset \"Smallest prime deficient number is classified correctly\" begin\n", " @test isdeficient(2)\n", " end\n", "\n", " @testset \"Smallest non-prime deficient number is classified correctly\" begin\n", " @test isdeficient(4)\n", " end\n", "\n", " @testset \"Medium deficient number is classified correctly\" begin\n", " @test isdeficient(32)\n", " end\n", "\n", " @testset \"Large deficient number is classified correctly\" begin\n", " @test isdeficient(33550337)\n", " end\n", "\n", " @testset \"Edge case (no factors other than itself) is classified correctly\" begin\n", " @test isdeficient(1)\n", " end\n", "end\n", "\n", "@testset \"Invalid inputs\" begin\n", "\n", " @testset \"Zero is rejected (not a natural number)\" begin\n", " @test_throws DomainError isdeficient(0)\n", " @test_throws DomainError isperfect(0)\n", " @test_throws DomainError isabundant(0)\n", " end\n", "\n", " @testset \"Negative integer is rejected (not a natural number)\" begin\n", " @test_throws DomainError isdeficient(-1)\n", " @test_throws DomainError isperfect(-1)\n", " @test_throws DomainError isabundant(-1)\n", " end\n", "end"]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Prepare submission\n",
"To submit your exercise, you need to save your solution in a file called `perfect-numbers.jl` before using the CLI.\n",
"You can either create it manually or use the following functions, which will automatically write every notebook cell that starts with `# submit` to the file `perfect-numbers.jl`.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# using Pkg; Pkg.add(\"Exercism\")\n",
"# using Exercism\n",
"# Exercism.create_submission(\"perfect-numbers\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Julia 1.3.0",
"language": "julia",
"name": "julia-1.3"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "1.3.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}