 # SteffenBauer's solution

## to Prime Factors in the Lua Track

Published at Jul 13 2018 · 0 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.

## 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!

## Running the tests

To run the tests, run the command `busted` from within the exercise directory.

## Further information

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.

## Source

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

## Submitting Incomplete Solutions

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

### prime-factors_spec.lua

``````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)``````
``````function PrimeFactors(input)
factors, n = {}, 2
while n <= input do
if input % n == 0 then
table.insert(factors, n)
input = input / n
else
n = n + (n==2 and 1 or 2)
end
end
return factors
end

return PrimeFactors``````