Some solutions for the julia path
This commit is contained in:
22
julia/collatz-conjecture/.exercism/config.json
Normal file
22
julia/collatz-conjecture/.exercism/config.json
Normal file
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"authors": [
|
||||
"SaschaMann"
|
||||
],
|
||||
"contributors": [
|
||||
"guilhermebodin"
|
||||
],
|
||||
"files": {
|
||||
"solution": [
|
||||
"collatz-conjecture.jl"
|
||||
],
|
||||
"test": [
|
||||
"runtests.jl"
|
||||
],
|
||||
"example": [
|
||||
".meta/example.jl"
|
||||
]
|
||||
},
|
||||
"blurb": "Calculate the number of steps to reach 1 using the Collatz conjecture.",
|
||||
"source": "Wikipedia",
|
||||
"source_url": "https://en.wikipedia.org/wiki/Collatz_conjecture"
|
||||
}
|
||||
1
julia/collatz-conjecture/.exercism/metadata.json
Normal file
1
julia/collatz-conjecture/.exercism/metadata.json
Normal file
@@ -0,0 +1 @@
|
||||
{"track":"julia","exercise":"collatz-conjecture","id":"3beda5faa27740749db53e30351afca0","url":"https://exercism.org/tracks/julia/exercises/collatz-conjecture","handle":"Kimawari","is_requester":true,"auto_approve":false}
|
||||
34
julia/collatz-conjecture/HELP.md
Normal file
34
julia/collatz-conjecture/HELP.md
Normal 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 collatz-conjecture.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/).
|
||||
51
julia/collatz-conjecture/README.md
Normal file
51
julia/collatz-conjecture/README.md
Normal file
@@ -0,0 +1,51 @@
|
||||
# Collatz Conjecture
|
||||
|
||||
Welcome to Collatz Conjecture on Exercism's Julia Track.
|
||||
If you need help running the tests or submitting your code, check out `HELP.md`.
|
||||
|
||||
## Introduction
|
||||
|
||||
One evening, you stumbled upon an old notebook filled with cryptic scribbles, as though someone had been obsessively chasing an idea.
|
||||
On one page, a single question stood out: **Can every number find its way to 1?**
|
||||
It was tied to something called the **Collatz Conjecture**, a puzzle that has baffled thinkers for decades.
|
||||
|
||||
The rules were deceptively simple.
|
||||
Pick any positive integer.
|
||||
|
||||
- If it's even, divide it by 2.
|
||||
- If it's odd, multiply it by 3 and add 1.
|
||||
|
||||
Then, repeat these steps with the result, continuing indefinitely.
|
||||
|
||||
Curious, you picked number 12 to test and began the journey:
|
||||
|
||||
12 ➜ 6 ➜ 3 ➜ 10 ➜ 5 ➜ 16 ➜ 8 ➜ 4 ➜ 2 ➜ 1
|
||||
|
||||
Counting from the second number (6), it took 9 steps to reach 1, and each time the rules repeated, the number kept changing.
|
||||
At first, the sequence seemed unpredictable — jumping up, down, and all over.
|
||||
Yet, the conjecture claims that no matter the starting number, we'll always end at 1.
|
||||
|
||||
It was fascinating, but also puzzling.
|
||||
Why does this always seem to work?
|
||||
Could there be a number where the process breaks down, looping forever or escaping into infinity?
|
||||
The notebook suggested solving this could reveal something profound — and with it, fame, [fortune][collatz-prize], and a place in history awaits whoever could unlock its secrets.
|
||||
|
||||
[collatz-prize]: https://mathprize.net/posts/collatz-conjecture/
|
||||
|
||||
## Instructions
|
||||
|
||||
Given a positive integer, return the number of steps it takes to reach 1 according to the rules of the Collatz Conjecture.
|
||||
|
||||
## Source
|
||||
|
||||
### Created by
|
||||
|
||||
- @SaschaMann
|
||||
|
||||
### Contributed to by
|
||||
|
||||
- @guilhermebodin
|
||||
|
||||
### Based on
|
||||
|
||||
Wikipedia - https://en.wikipedia.org/wiki/Collatz_conjecture
|
||||
23
julia/collatz-conjecture/collatz-conjecture.jl
Normal file
23
julia/collatz-conjecture/collatz-conjecture.jl
Normal file
@@ -0,0 +1,23 @@
|
||||
function is_even(n::Int)
|
||||
return n % 2 == 0
|
||||
end
|
||||
|
||||
function is_odd(n::Int)
|
||||
return n % 2 != 0
|
||||
end
|
||||
|
||||
function collatz_steps(n::Int)
|
||||
if n <= 0
|
||||
throw(DomainError("Input must be a positive integer."))
|
||||
end
|
||||
steps = 0
|
||||
while n != 1
|
||||
if is_even(n)
|
||||
n ÷= 2
|
||||
else
|
||||
n = 3 * n + 1
|
||||
end
|
||||
steps += 1
|
||||
end
|
||||
return steps
|
||||
end
|
||||
15
julia/collatz-conjecture/runtests.jl
Normal file
15
julia/collatz-conjecture/runtests.jl
Normal file
@@ -0,0 +1,15 @@
|
||||
using Test
|
||||
|
||||
include("collatz-conjecture.jl")
|
||||
|
||||
@testset verbose = true "tests" begin
|
||||
# canonical data
|
||||
@testset "Canonical data" begin
|
||||
@test collatz_steps(1) == 0
|
||||
@test collatz_steps(16) == 4
|
||||
@test collatz_steps(12) == 9
|
||||
@test collatz_steps(1000000) == 152
|
||||
@test_throws DomainError collatz_steps(0)
|
||||
@test_throws DomainError collatz_steps(-15)
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user