Skip to content

Commit b4d2836

Browse files
authored
Add prime-factors (#353)
1 parent 519e481 commit b4d2836

File tree

7 files changed

+163
-0
lines changed

7 files changed

+163
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,14 @@
385385
"prerequisites": [],
386386
"difficulty": 3
387387
},
388+
{
389+
"slug": "prime-factors",
390+
"name": "Prime Factors",
391+
"uuid": "bd85cab1-c5e1-46ac-9faf-a815510fdfcd",
392+
"practices": [],
393+
"prerequisites": [],
394+
"difficulty": 2
395+
},
388396
{
389397
"slug": "protein-translation",
390398
"name": "Protein Translation",
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Instructions
2+
3+
Compute the prime factors of a given natural number.
4+
5+
A prime number is only evenly divisible by itself and 1.
6+
7+
Note that 1 is not a prime number.
8+
9+
## Example
10+
11+
What are the prime factors of 60?
12+
13+
- Our first divisor is 2.
14+
2 goes into 60, leaving 30.
15+
- 2 goes into 30, leaving 15.
16+
- 2 doesn't go cleanly into 15.
17+
So let's move on to our next divisor, 3.
18+
- 3 goes cleanly into 15, leaving 5.
19+
- 3 does not go cleanly into 5.
20+
The next possible factor is 4.
21+
- 4 does not go cleanly into 5.
22+
The next possible factor is 5.
23+
- 5 does go cleanly into 5.
24+
- We're left only with 1, so now, we're done.
25+
26+
Our successful divisors in that computation represent the list of prime factors of 60: 2, 2, 3, and 5.
27+
28+
You can check this yourself:
29+
30+
```text
31+
2 * 2 * 3 * 5
32+
= 4 * 15
33+
= 60
34+
```
35+
36+
Success!
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"BNAndras"
4+
],
5+
"files": {
6+
"solution": [
7+
"prime-factors.coffee"
8+
],
9+
"test": [
10+
"prime-factors.spec.coffee"
11+
],
12+
"example": [
13+
".meta/example.coffee"
14+
]
15+
},
16+
"blurb": "Compute the prime factors of a given natural number.",
17+
"source": "The Prime Factors Kata by Uncle Bob",
18+
"source_url": "https://web.archive.org/web/20221026171801/http://butunclebob.com/ArticleS.UncleBob.ThePrimeFactorsKata"
19+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class PrimeFactors
2+
@factors: (value) ->
3+
factors = []
4+
divisor = 2
5+
while value > 1
6+
while value % divisor == 0
7+
factors.push divisor
8+
value /= divisor
9+
divisor += 1
10+
factors
11+
12+
module.exports = PrimeFactors
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[924fc966-a8f5-4288-82f2-6b9224819ccd]
13+
description = "no factors"
14+
15+
[17e30670-b105-4305-af53-ddde182cb6ad]
16+
description = "prime number"
17+
18+
[238d57c8-4c12-42ef-af34-ae4929f94789]
19+
description = "another prime number"
20+
21+
[f59b8350-a180-495a-8fb1-1712fbee1158]
22+
description = "square of a prime"
23+
24+
[756949d3-3158-4e3d-91f2-c4f9f043ee70]
25+
description = "product of first prime"
26+
27+
[bc8c113f-9580-4516-8669-c5fc29512ceb]
28+
description = "cube of a prime"
29+
30+
[7d6a3300-a4cb-4065-bd33-0ced1de6cb44]
31+
description = "product of second prime"
32+
33+
[073ac0b2-c915-4362-929d-fc45f7b9a9e4]
34+
description = "product of third prime"
35+
36+
[6e0e4912-7fb6-47f3-a9ad-dbcd79340c75]
37+
description = "product of first and second prime"
38+
39+
[00485cd3-a3fe-4fbe-a64a-a4308fc1f870]
40+
description = "product of primes and non-primes"
41+
42+
[02251d54-3ca1-4a9b-85e1-b38f4b0ccb91]
43+
description = "product of primes"
44+
45+
[070cf8dc-e202-4285-aa37-8d775c9cd473]
46+
description = "factors include a large prime"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class PrimeFactors
2+
@factors: (value) ->
3+
4+
module.exports = PrimeFactors
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
PrimeFactors = require './prime-factors'
2+
3+
describe 'Prime Factors', ->
4+
it 'no factors', ->
5+
expect(PrimeFactors.factors 1).toEqual []
6+
7+
xit 'prime number', ->
8+
expect(PrimeFactors.factors 2).toEqual [2]
9+
10+
xit 'another prime number', ->
11+
expect(PrimeFactors.factors 3).toEqual [3]
12+
13+
xit 'square of a prime', ->
14+
expect(PrimeFactors.factors 9).toEqual [3, 3]
15+
16+
xit 'product of first prime', ->
17+
expect(PrimeFactors.factors 4).toEqual [2, 2]
18+
19+
xit 'cube of a prime', ->
20+
expect(PrimeFactors.factors 8).toEqual [2, 2, 2]
21+
22+
xit 'product of second prime', ->
23+
expect(PrimeFactors.factors 27).toEqual [3, 3, 3]
24+
25+
xit 'product of third prime', ->
26+
expect(PrimeFactors.factors 625).toEqual [5, 5, 5, 5]
27+
28+
xit 'product of first and second prime', ->
29+
expect(PrimeFactors.factors 6).toEqual [2, 3]
30+
31+
xit 'product of primes and non-primes', ->
32+
expect(PrimeFactors.factors 12).toEqual [2, 2, 3]
33+
34+
xit 'product of primes', ->
35+
expect(PrimeFactors.factors 901255).toEqual [5, 17, 23, 461]
36+
37+
xit 'factors include a large prime', ->
38+
expect(PrimeFactors.factors 93819012551).toEqual [11, 9539, 894119]

0 commit comments

Comments
 (0)