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

69 lines
4.1 KiB
Plaintext

{
"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
}