Published at Jul 13 2018
·
2 comments

Instructions

Test suite

Solution

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.

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!

To run the tests, run the command `busted`

from within the exercise directory.

For more detailed information about the Lua track, including how to get help if you're having trouble, please visit the exercism.io Lua language page.

The Prime Factors Kata by Uncle Bob http://butunclebob.com/ArticleS.UncleBob.ThePrimeFactorsKata

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

```
local prime_factors = require('prime-factors')
describe('prime-factors', function()
it('returns an empty array for 1', function()
assert.are.same({}, prime_factors(1))
end)
it('factors 2', function()
assert.are.same({ 2 }, prime_factors(2))
end)
it('factors 3', function()
assert.are.same({ 3 }, prime_factors(3))
end)
it('factors 4', function()
assert.are.same({ 2, 2 }, prime_factors(4))
end)
it('factors 6', function()
assert.are.same({ 2, 3 }, prime_factors(6))
end)
it('factors 8', function()
assert.are.same({ 2, 2, 2 }, prime_factors(8))
end)
it('factors 9', function()
assert.are.same({ 3, 3 }, prime_factors(9))
end)
it('factors 27', function()
assert.are.same({ 3, 3, 3 }, prime_factors(27))
end)
it('factors 625', function()
assert.are.same({ 5, 5, 5, 5 }, prime_factors(625))
end)
it('factors 901255', function()
assert.are.same({ 5, 17, 23, 461 }, prime_factors(901255))
end)
it('factors 93819012551', function()
assert.are.same({ 11, 9539, 894119 }, prime_factors(93819012551))
end)
end)
```

```
return function(n)
local factor = 2
local prime_factors = {}
while n > 1 do
if n % factor == 0 then
n = n / factor
table.insert(prime_factors, factor)
else
factor = factor + 1
end
end
return prime_factors
end
```

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

- What compromises have been made?
- Are there new concepts here that you could read more about to improve your understanding?

Level up your programming skills with 3,092 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments

I was pretty sure I was going to have to generate a list of prime numbers, but if you traverse the possible prime factors in ascending order you don't have to explicitly check for primality -- every divisor will automatically be prime. Extremely cool!

dude you're right, didn't even think of that