forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New exercise: prime-factors (JuliaLang#184)
Add files via upload Update config.json Delete runtests.jl Add files via upload Update config.json this is a trivial syntax error. Co-Authored-By: Sascha Mann <git@mail.saschamann.eu> Update config.json Co-Authored-By: Sascha Mann <git@mail.saschamann.eu> Update exercises/prime-factors/example.jl Co-Authored-By: Sascha Mann <git@mail.saschamann.eu> Update prime-factors.jl Update prime-factors.jl Update exercises/prime-factors/runtests.jl Co-Authored-By: Sascha Mann <git@mail.saschamann.eu> Add files via upload Delete prime-factors.ipynb Add files via upload Update example.jl Update example.jl Delete README.md Add files via upload Rename description.md to README.md Cleanup
- Loading branch information
1 parent
1233e6a
commit f3b9406
Showing
6 changed files
with
185 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
function isprime(n) | ||
if n == 1 | ||
return false | ||
elseif n == 2 | ||
return true | ||
else | ||
for i = 2:(n ^ (0.5)) + 1 | ||
if n % i == 0 | ||
return false | ||
end | ||
end | ||
return true | ||
end | ||
end | ||
|
||
function prime_factors(n) | ||
count = 0 | ||
final = [1] | ||
k = n | ||
if n == 1 | ||
return [] | ||
end | ||
while k != 1 | ||
count += 1 | ||
if k % count == 0 && isprime(count) == true | ||
k = k/count | ||
final = [final; [count]] | ||
count = 0 | ||
end | ||
|
||
|
||
end | ||
return final[2:end] | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# canonical data version 1.1.0 | ||
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 |