Skip to content

Commit

Permalink
New exercise: prime-factors (JuliaLang#184)
Browse files Browse the repository at this point in the history
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
NandVinchhi authored and SaschaMann committed Dec 9, 2019
1 parent 1233e6a commit f3b9406
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 0 deletions.
11 changes: 11 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,17 @@
"loops",
"strings"
]
},
{
"slug": "prime-factors",
"uuid": "b6b7e95b-c4ca-438c-a5e7-d76cb44e5b19",
"core": false,
"unlocked_by": null,
"difficulty": 1,
"topics": [
"math",
"integers"
]
}
]
}
40 changes: 40 additions & 0 deletions exercises/prime-factors/README.md
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.
34 changes: 34 additions & 0 deletions exercises/prime-factors/example.jl
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
68 changes: 68 additions & 0 deletions exercises/prime-factors/prime-factors.ipynb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions exercises/prime-factors/prime-factors.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

31 changes: 31 additions & 0 deletions exercises/prime-factors/runtests.jl
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

0 comments on commit f3b9406

Please sign in to comment.