Initial upload
This commit is contained in:
1
julia/prime-factors/.exercism/metadata.json
Normal file
1
julia/prime-factors/.exercism/metadata.json
Normal file
@@ -0,0 +1 @@
|
||||
{"track":"julia","exercise":"prime-factors","id":"5198169256b2415da05999facf51ba03","url":"https://exercism.io/my/solutions/5198169256b2415da05999facf51ba03","handle":"halfdan","is_requester":true,"auto_approve":false}
|
40
julia/prime-factors/README.md
Normal file
40
julia/prime-factors/README.md
Normal file
@@ -0,0 +1,40 @@
|
||||
# Prime Factors
|
||||
|
||||
Compute the prime factors of a given natural number.
|
||||
|
||||
A prime number is only evenly divisible by itself and 1.
|
||||
|
||||
Note that 1 is not a prime number.
|
||||
|
||||
## Example
|
||||
|
||||
What are the prime factors of 60?
|
||||
|
||||
- Our first divisor is 2. 2 goes into 60, leaving 30.
|
||||
- 2 goes into 30, leaving 15.
|
||||
- 2 doesn't go cleanly into 15. So let's move on to our next divisor, 3.
|
||||
- 3 goes cleanly into 15, leaving 5.
|
||||
- 3 does not go cleanly into 5. The next possible factor is 4.
|
||||
- 4 does not go cleanly into 5. The next possible factor is 5.
|
||||
- 5 does go cleanly into 5.
|
||||
- We're left only with 1, so now, we're done.
|
||||
|
||||
Our successful divisors in that computation represent the list of prime
|
||||
factors of 60: 2, 2, 3, and 5.
|
||||
|
||||
You can check this yourself:
|
||||
|
||||
- 2 * 2 * 3 * 5
|
||||
- = 4 * 15
|
||||
- = 60
|
||||
- Success!
|
||||
|
||||
## Source
|
||||
|
||||
The Prime Factors Kata by Uncle Bob [http://butunclebob.com/ArticleS.UncleBob.ThePrimeFactorsKata](http://butunclebob.com/ArticleS.UncleBob.ThePrimeFactorsKata)
|
||||
|
||||
## 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/prime-factors/prime-factors.ipynb
Normal file
68
julia/prime-factors/prime-factors.ipynb
Normal file
@@ -0,0 +1,68 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": ["# Prime Factors\n", "\n", "Compute the prime factors of a given natural number.\n", "\n", "A prime number is only evenly divisible by itself and 1.\n", "\n", "Note that 1 is not a prime number.\n", "\n", "## Example\n", "\n", "What are the prime factors of 60?\n", "\n", "- Our first divisor is 2. 2 goes into 60, leaving 30.\n", "- 2 goes into 30, leaving 15.\n", " - 2 doesn't go cleanly into 15. So let's move on to our next divisor, 3.\n", "- 3 goes cleanly into 15, leaving 5.\n", " - 3 does not go cleanly into 5. The next possible factor is 4.\n", " - 4 does not go cleanly into 5. The next possible factor is 5.\n", "- 5 does go cleanly into 5.\n", "- We're left only with 1, so now, we're done.\n", "\n", "Our successful divisors in that computation represent the list of prime\n", "factors of 60: 2, 2, 3, and 5.\n", "\n", "You can check this yourself:\n", "\n", "- 2 * 2 * 3 * 5\n", "- = 4 * 15\n", "- = 60\n", "- Success!\n", "\n", "## Source\n", "\n", "The Prime Factors Kata by Uncle Bob [http://butunclebob.com/ArticleS.UncleBob.ThePrimeFactorsKata](http://butunclebob.com/ArticleS.UncleBob.ThePrimeFactorsKata)\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": ["# include(\"prime-factors.jl\")\n", "using Test\n", "\n", "@testset \"no factors\" begin\n", " @test prime_factors(1) == []\n", "end\n", "\n", "@testset \"prime number\" begin\n", " @test prime_factors(2) == [2]\n", "end\n", "\n", "@testset \"square of a prime\" begin\n", " @test prime_factors(9) == [3, 3]\n", "end\n", "\n", "@testset \"cube of a prime\" begin\n", " @test prime_factors(8) == [2, 2, 2]\n", "end\n", "\n", "@testset \"product of primes and non-primes\" begin\n", " @test prime_factors(12) == [2, 2, 3]\n", "end\n", "\n", "@testset \"product of primes\" begin\n", " @test prime_factors(901255) == [5, 17, 23, 461]\n", "end\n", "\n", "@testset \"factors include a large prime\" begin\n", " @test prime_factors(93819012551) == [11, 9539, 894119]\n", "end"]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Prepare submission\n",
|
||||
"To submit your exercise, you need to save your solution in a file called `prime-factors.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 `prime-factors.jl`.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# using Pkg; Pkg.add(\"Exercism\")\n",
|
||||
"# using Exercism\n",
|
||||
"# Exercism.create_submission(\"prime-factors\")"
|
||||
]
|
||||
}
|
||||
],
|
||||
"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
|
||||
}
|
13
julia/prime-factors/prime-factors.jl
Normal file
13
julia/prime-factors/prime-factors.jl
Normal file
@@ -0,0 +1,13 @@
|
||||
function prime_factors(n::Int)
|
||||
primes = []
|
||||
while n != 1 && n > 1
|
||||
for i in 2:n
|
||||
if n % i == 0
|
||||
push!(primes, i)
|
||||
n = div(n, i)
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
primes
|
||||
end
|
30
julia/prime-factors/runtests.jl
Normal file
30
julia/prime-factors/runtests.jl
Normal file
@@ -0,0 +1,30 @@
|
||||
include("prime-factors.jl")
|
||||
using Test
|
||||
|
||||
@testset "no factors" begin
|
||||
@test prime_factors(1) == []
|
||||
end
|
||||
|
||||
@testset "prime number" begin
|
||||
@test prime_factors(2) == [2]
|
||||
end
|
||||
|
||||
@testset "square of a prime" begin
|
||||
@test prime_factors(9) == [3, 3]
|
||||
end
|
||||
|
||||
@testset "cube of a prime" begin
|
||||
@test prime_factors(8) == [2, 2, 2]
|
||||
end
|
||||
|
||||
@testset "product of primes and non-primes" begin
|
||||
@test prime_factors(12) == [2, 2, 3]
|
||||
end
|
||||
|
||||
@testset "product of primes" begin
|
||||
@test prime_factors(901255) == [5, 17, 23, 461]
|
||||
end
|
||||
|
||||
@testset "factors include a large prime" begin
|
||||
@test prime_factors(93819012551) == [11, 9539, 894119]
|
||||
end
|
Reference in New Issue
Block a user