Some solutions for the julia path

This commit is contained in:
2025-08-04 19:44:03 +02:00
parent 5c52e8e34d
commit 8296c79f68
104 changed files with 4373 additions and 0 deletions

View File

@@ -0,0 +1,25 @@
{
"authors": [
"Akshat-mehrotra"
],
"contributors": [
"cmcaine",
"miguelraz",
"SaschaMann",
"logankilpatrick"
],
"files": {
"solution": [
"perfect-numbers.jl"
],
"test": [
"runtests.jl"
],
"example": [
".meta/example.jl"
]
},
"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/"
}

View File

@@ -0,0 +1 @@
{"track":"julia","exercise":"perfect-numbers","id":"63be4afee8e04dce908a953310580ba6","url":"https://exercism.org/tracks/julia/exercises/perfect-numbers","handle":"Kimawari","is_requester":true,"auto_approve":false}

View File

@@ -0,0 +1,34 @@
# Help
## Running the tests
To run the tests, run this command from within the exercise directory:
```bash
$ julia runtests.jl
```
## Submitting your solution
You can submit your solution using the `exercism submit perfect-numbers.jl` command.
This command will upload your solution to the Exercism website and print the solution page's URL.
It's possible to submit an incomplete solution which allows you to:
- See how others have completed the exercise
- Request help from a mentor
## Need to get help?
If you'd like help solving the exercise, check the following pages:
- The [Julia track's documentation](https://exercism.org/docs/tracks/julia)
- The [Julia track's programming category on the forum](https://forum.exercism.org/c/programming/julia)
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
To get help if you're having trouble, we recommend that you submit your code and request mentoring :)
If you don't want to do that for whatever reason, then you can find the wider Julia community channels [here](https://julialang.org/community/).

View File

@@ -0,0 +1,61 @@
# Perfect Numbers
Welcome to Perfect Numbers on Exercism's Julia Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
## 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
## Source
### Created by
- @Akshat-mehrotra
### Contributed to by
- @cmcaine
- @miguelraz
- @SaschaMann
- @logankilpatrick
### Based on
Taken from Chapter 2 of Functional Thinking by Neal Ford. - https://www.oreilly.com/library/view/functional-thinking/9781449365509/

View File

@@ -0,0 +1,14 @@
function get_divisors(n)
n > 0 || throw(DomainError(n))
return sum(i for i in 1:n÷2 if iszero(n % i))
end
function isperfect(number)
return get_divisors(number) number
end
function isabundant(number)
return get_divisors(number) > number
end
function isdeficient(number)
return get_divisors(number) < number
end

View File

@@ -0,0 +1,88 @@
using Test
include("perfect-numbers.jl")
@testset verbose = true "tests" begin
@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
@testset "Correctly handles non-perfect numbers" begin
@test !isperfect(12)
@test !isperfect(4)
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
@testset "Correctly handles non-abundant numbers" begin
@test !isabundant(6)
@test !isabundant(32)
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
@testset "Correctly handles non-deficient numbers" begin
@test !isdeficient(28)
@test !isdeficient(30)
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
end