Initial upload
This commit is contained in:
1
julia/perfect-numbers/.exercism/metadata.json
Normal file
1
julia/perfect-numbers/.exercism/metadata.json
Normal file
@@ -0,0 +1 @@
|
||||
{"track":"julia","exercise":"perfect-numbers","id":"6aa6745ac14f4533b0a7b20f97053979","url":"https://exercism.io/my/solutions/6aa6745ac14f4533b0a7b20f97053979","handle":"halfdan","is_requester":true,"auto_approve":false}
|
28
julia/perfect-numbers/README.md
Normal file
28
julia/perfect-numbers/README.md
Normal file
@@ -0,0 +1,28 @@
|
||||
# Perfect Numbers
|
||||
|
||||
Determine if a number is perfect, abundant, or deficient based on
|
||||
Nicomachus' (60 - 120 CE) classification scheme for natural numbers.
|
||||
|
||||
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
|
||||
|
||||
- **Perfect**: aliquot sum = number
|
||||
- 6 is a perfect number because (1 + 2 + 3) = 6
|
||||
- 28 is a perfect number because (1 + 2 + 4 + 7 + 14) = 28
|
||||
- **Abundant**: aliquot sum > number
|
||||
- 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**: aliquot sum < number
|
||||
- 8 is a deficient number because (1 + 2 + 4) = 7
|
||||
- Prime numbers are deficient
|
||||
|
||||
Implement a way to determine whether a given number is **perfect**, **abundant** or **deficient**.
|
||||
|
||||
## Source
|
||||
|
||||
Taken from Chapter 2 of Functional Thinking by Neal Ford. [http://shop.oreilly.com/product/0636920029687.do](http://shop.oreilly.com/product/0636920029687.do)
|
||||
|
||||
## Version compatibility
|
||||
This exercise has been tested on Julia versions >=1.0.
|
||||
|
||||
## Submitting Incomplete Solutions
|
||||
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
|
68
julia/perfect-numbers/perfect-numbers.ipynb
Normal file
68
julia/perfect-numbers/perfect-numbers.ipynb
Normal file
@@ -0,0 +1,68 @@
|
||||
{
|
||||
"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
|
||||
}
|
8
julia/perfect-numbers/perfect-numbers.jl
Normal file
8
julia/perfect-numbers/perfect-numbers.jl
Normal file
@@ -0,0 +1,8 @@
|
||||
function aliquot_sum(n)
|
||||
n <= 0 && throw(DomainError("Aliquot sum only exists for natural numbers"))
|
||||
sum([x for x in 1:div(n, 2) if mod(n, x) == 0])
|
||||
end
|
||||
|
||||
isperfect(n) = aliquot_sum(n) == n
|
||||
isabundant(n) = aliquot_sum(n) > n
|
||||
isdeficient(n) = aliquot_sum(n) < n
|
71
julia/perfect-numbers/runtests.jl
Normal file
71
julia/perfect-numbers/runtests.jl
Normal file
@@ -0,0 +1,71 @@
|
||||
using Test
|
||||
|
||||
include("perfect-numbers.jl")
|
||||
|
||||
@testset "Perfect numbers" begin
|
||||
|
||||
@testset "Smallest perfect number is classified correctly" begin
|
||||
@test isperfect(6)
|
||||
end
|
||||
|
||||
@testset "Medium perfect number is classified correctly" begin
|
||||
@test isperfect(28)
|
||||
end
|
||||
|
||||
@testset "Large perfect number is classified correctly" begin
|
||||
@test isperfect(33550336)
|
||||
end
|
||||
end
|
||||
|
||||
@testset "Abundant numbers" begin
|
||||
|
||||
@testset "Smallest abundant number is classified correctly" begin
|
||||
@test isabundant(12)
|
||||
end
|
||||
|
||||
@testset "Medium abundant number is classified correctly" begin
|
||||
@test isabundant(30)
|
||||
end
|
||||
|
||||
@testset "Large abundant number is classified correctly" begin
|
||||
@test isabundant(33550335)
|
||||
end
|
||||
end
|
||||
|
||||
@testset "Deficient numbers" begin
|
||||
|
||||
@testset "Smallest prime deficient number is classified correctly" begin
|
||||
@test isdeficient(2)
|
||||
end
|
||||
|
||||
@testset "Smallest non-prime deficient number is classified correctly" begin
|
||||
@test isdeficient(4)
|
||||
end
|
||||
|
||||
@testset "Medium deficient number is classified correctly" begin
|
||||
@test isdeficient(32)
|
||||
end
|
||||
|
||||
@testset "Large deficient number is classified correctly" begin
|
||||
@test isdeficient(33550337)
|
||||
end
|
||||
|
||||
@testset "Edge case (no factors other than itself) is classified correctly" begin
|
||||
@test isdeficient(1)
|
||||
end
|
||||
end
|
||||
|
||||
@testset "Invalid inputs" begin
|
||||
|
||||
@testset "Zero is rejected (not a natural number)" begin
|
||||
@test_throws DomainError isdeficient(0)
|
||||
@test_throws DomainError isperfect(0)
|
||||
@test_throws DomainError isabundant(0)
|
||||
end
|
||||
|
||||
@testset "Negative integer is rejected (not a natural number)" begin
|
||||
@test_throws DomainError isdeficient(-1)
|
||||
@test_throws DomainError isperfect(-1)
|
||||
@test_throws DomainError isabundant(-1)
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user