Avatar of JosePadron

JosePadron's solution

to Prime Factors in the Lua Track

Published at Jul 13 2018 · 1 comment
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)
--------------------------------------------------------------------
--! @file
--! @brief Compute the prime factors of a given natural number.
---------------------------------------------------------------------
return function(number)
  local divisor = 2
  local factors = {}

  while(number > 1) do
    while (0 == number % divisor) do
      number = math.floor(number / divisor)
      table.insert(factors, divisor)
    end
    divisor = divisor + 1
  end

  return factors
end

Community comments

Find this solution interesting? Ask the author a question to learn more.
Avatar of ryanplusplus

Good idea to use a nested while. That works the same but makes more sense than my solution.

What can you learn from this solution?

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?