Skip to content

Commit 2d0acee

Browse files
committed
[gleam] complete prime-factors
1 parent 7537aba commit 2d0acee

File tree

7 files changed

+207
-0
lines changed

7 files changed

+207
-0
lines changed

gleam/prime-factors/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
*.beam
2+
*.ez
3+
build
4+
erl_crash.dump

gleam/prime-factors/HELP.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Help
2+
3+
## Running the tests
4+
5+
To run the tests, run the command `gleam test` from within the exercise directory.
6+
7+
## Submitting your solution
8+
9+
You can submit your solution using the `exercism submit src/prime_factors.gleam` command.
10+
This command will upload your solution to the Exercism website and print the solution page's URL.
11+
12+
It's possible to submit an incomplete solution which allows you to:
13+
14+
- See how others have completed the exercise
15+
- Request help from a mentor
16+
17+
## Need to get help?
18+
19+
If you'd like help solving the exercise, check the following pages:
20+
21+
- The [Gleam track's documentation](https://exercism.org/docs/tracks/gleam)
22+
- The [Gleam track's programming category on the forum](https://forum.exercism.org/c/programming/gleam)
23+
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
24+
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
25+
26+
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
27+
28+
To get help if you're having trouble, you can use one of the following resources:
29+
30+
- [gleam.run](https://gleam.run/documentation/) is the gleam official documentation.
31+
- [Discord](https://discord.gg/Fm8Pwmy) is the discord channel.
32+
- [StackOverflow](https://stackoverflow.com/questions/tagged/gleam) can be used to search for your problem and see if it has been answered already. You can also ask and answer questions.

gleam/prime-factors/README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Prime Factors
2+
3+
Welcome to Prime Factors on Exercism's Gleam Track.
4+
If you need help running the tests or submitting your code, check out `HELP.md`.
5+
6+
## Instructions
7+
8+
Compute the prime factors of a given natural number.
9+
10+
A prime number is only evenly divisible by itself and 1.
11+
12+
Note that 1 is not a prime number.
13+
14+
## Example
15+
16+
What are the prime factors of 60?
17+
18+
- Our first divisor is 2.
19+
2 goes into 60, leaving 30.
20+
- 2 goes into 30, leaving 15.
21+
- 2 doesn't go cleanly into 15.
22+
So let's move on to our next divisor, 3.
23+
- 3 goes cleanly into 15, leaving 5.
24+
- 3 does not go cleanly into 5.
25+
The next possible factor is 4.
26+
- 4 does not go cleanly into 5.
27+
The next possible factor is 5.
28+
- 5 does go cleanly into 5.
29+
- We're left only with 1, so now, we're done.
30+
31+
Our successful divisors in that computation represent the list of prime factors of 60: 2, 2, 3, and 5.
32+
33+
You can check this yourself:
34+
35+
```text
36+
2 * 2 * 3 * 5
37+
= 4 * 15
38+
= 60
39+
```
40+
41+
Success!
42+
43+
## Source
44+
45+
### Created by
46+
47+
- @jiegillet
48+
49+
### Based on
50+
51+
The Prime Factors Kata by Uncle Bob - https://web.archive.org/web/20221026171801/http://butunclebob.com/ArticleS.UncleBob.ThePrimeFactorsKata

gleam/prime-factors/gleam.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
name = "prime_factors"
2+
version = "0.1.0"
3+
4+
[dependencies]
5+
gleam_bitwise = "~> 1.2"
6+
gleam_otp = "~> 0.7 or ~> 1.0"
7+
gleam_stdlib = "~> 0.32 or ~> 1.0"
8+
simplifile = "~> 1.0"
9+
gleam_erlang = ">= 0.25.0 and < 1.0.0"
10+
11+
[dev-dependencies]
12+
exercism_test_runner = "~> 1.4"

gleam/prime-factors/manifest.toml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# This file was generated by Gleam
2+
# You typically do not need to edit this file
3+
4+
packages = [
5+
{ name = "argv", version = "1.0.2", build_tools = ["gleam"], requirements = [], otp_app = "argv", source = "hex", outer_checksum = "BA1FF0929525DEBA1CE67256E5ADF77A7CDDFE729E3E3F57A5BDCAA031DED09D" },
6+
{ name = "exercism_test_runner", version = "1.8.0", build_tools = ["gleam"], requirements = ["argv", "gap", "glance", "gleam_community_ansi", "gleam_erlang", "gleam_json", "gleam_stdlib", "simplifile"], otp_app = "exercism_test_runner", source = "hex", outer_checksum = "B944D89A9D049897DF28C63D595D89CB54D8C407D06EFFCE4CDA8C3EC1C9F51E" },
7+
{ name = "filepath", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "EFB6FF65C98B2A16378ABC3EE2B14124168C0CE5201553DE652E2644DCFDB594" },
8+
{ name = "gap", version = "1.1.3", build_tools = ["gleam"], requirements = ["gleam_community_ansi", "gleam_stdlib"], otp_app = "gap", source = "hex", outer_checksum = "6EF5E3B523FDFBC317E9EA28D5163EE04744A97C007106F90207569789612291" },
9+
{ name = "glance", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "glexer"], otp_app = "glance", source = "hex", outer_checksum = "1510D4A03C28880E62974389E5BF1A5A185036BA07392F1D769620706A9E042F" },
10+
{ name = "gleam_bitwise", version = "1.3.1", build_tools = ["gleam"], requirements = [], otp_app = "gleam_bitwise", source = "hex", outer_checksum = "B36E1D3188D7F594C7FD4F43D0D2CE17561DE896202017548578B16FE1FE9EFC" },
11+
{ name = "gleam_community_ansi", version = "1.4.1", build_tools = ["gleam"], requirements = ["gleam_community_colour", "gleam_stdlib"], otp_app = "gleam_community_ansi", source = "hex", outer_checksum = "4CD513FC62523053E62ED7BAC2F36136EC17D6A8942728250A9A00A15E340E4B" },
12+
{ name = "gleam_community_colour", version = "1.4.0", build_tools = ["gleam"], requirements = ["gleam_json", "gleam_stdlib"], otp_app = "gleam_community_colour", source = "hex", outer_checksum = "795964217EBEDB3DA656F5EB8F67D7AD22872EB95182042D3E7AFEF32D3FD2FE" },
13+
{ name = "gleam_erlang", version = "0.27.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "DE468F676D71B313C6C8C5334425CFCF827837333F8AB47B64D8A6D7AA40185D" },
14+
{ name = "gleam_json", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib", "thoas"], otp_app = "gleam_json", source = "hex", outer_checksum = "9063D14D25406326C0255BDA0021541E797D8A7A12573D849462CAFED459F6EB" },
15+
{ name = "gleam_otp", version = "0.12.1", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "BFACC1513410DF5A1617169A9CD7EA334973AC71D860A17574BA7B2EADD89A6F" },
16+
{ name = "gleam_stdlib", version = "0.40.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "86606B75A600BBD05E539EB59FABC6E307EEEA7B1E5865AFB6D980A93BCB2181" },
17+
{ name = "glexer", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "glexer", source = "hex", outer_checksum = "BD477AD657C2B637FEF75F2405FAEFFA533F277A74EF1A5E17B55B1178C228FB" },
18+
{ name = "simplifile", version = "1.7.0", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "1D5DFA3A2F9319EC85825F6ED88B8E449F381B0D55A62F5E61424E748E7DDEB0" },
19+
{ name = "thoas", version = "1.2.1", build_tools = ["rebar3"], requirements = [], otp_app = "thoas", source = "hex", outer_checksum = "E38697EDFFD6E91BD12CEA41B155115282630075C2A727E7A6B2947F5408B86A" },
20+
]
21+
22+
[requirements]
23+
exercism_test_runner = { version = "~> 1.4" }
24+
gleam_bitwise = { version = "~> 1.2" }
25+
gleam_erlang = { version = ">= 0.25.0 and < 1.0.0" }
26+
gleam_otp = { version = "~> 0.7 or ~> 1.0" }
27+
gleam_stdlib = { version = "~> 0.32 or ~> 1.0" }
28+
simplifile = { version = "~> 1.0" }
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import gleam/list
2+
3+
pub fn factors(value: Int) -> List(Int) {
4+
do_factors(value, 2, [])
5+
}
6+
7+
fn do_factors(n: Int, i: Int, factors: List(Int)) -> List(Int) {
8+
case i <= n, n % i == 0 {
9+
False, _ -> factors
10+
_, True -> do_factors(n / i, i, list.append(factors, [i]))
11+
_, False -> do_factors(n, i + 1, factors)
12+
}
13+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import exercism/should
2+
import exercism/test_runner
3+
import prime_factors
4+
5+
pub fn main() {
6+
test_runner.main()
7+
}
8+
9+
pub fn no_factors_test() {
10+
prime_factors.factors(1)
11+
|> should.equal([])
12+
}
13+
14+
pub fn prime_number_test() {
15+
prime_factors.factors(2)
16+
|> should.equal([2])
17+
}
18+
19+
pub fn another_prime_number_test() {
20+
prime_factors.factors(3)
21+
|> should.equal([3])
22+
}
23+
24+
pub fn square_of_a_prime_test() {
25+
prime_factors.factors(9)
26+
|> should.equal([3, 3])
27+
}
28+
29+
pub fn product_of_first_prime_test() {
30+
prime_factors.factors(4)
31+
|> should.equal([2, 2])
32+
}
33+
34+
pub fn cube_of_a_prime_test() {
35+
prime_factors.factors(8)
36+
|> should.equal([2, 2, 2])
37+
}
38+
39+
pub fn product_of_second_prime_test() {
40+
prime_factors.factors(27)
41+
|> should.equal([3, 3, 3])
42+
}
43+
44+
pub fn product_of_third_prime_test() {
45+
prime_factors.factors(625)
46+
|> should.equal([5, 5, 5, 5])
47+
}
48+
49+
pub fn product_of_first_and_second_prime_test() {
50+
prime_factors.factors(6)
51+
|> should.equal([2, 3])
52+
}
53+
54+
pub fn product_of_primes_and_non_primes_test() {
55+
prime_factors.factors(12)
56+
|> should.equal([2, 2, 3])
57+
}
58+
59+
pub fn product_of_primes_test() {
60+
prime_factors.factors(901_255)
61+
|> should.equal([5, 17, 23, 461])
62+
}
63+
64+
pub fn factors_include_a_large_prime_test() {
65+
prime_factors.factors(93_819_012_551)
66+
|> should.equal([11, 9539, 894_119])
67+
}

0 commit comments

Comments
 (0)