tags:

views:

90

answers:

2

Two quick questions (I hope...) with the following code. The script below checks if a number is prime, and if not, returns all the factors for that number, otherwise it just returns that the number prime. Pay no attention to the zs. stuff in the script, for that is client specific and has no bearing on script functionality.

The script itself works almost wonderfully, except for two minor details - the first being the factor list doesn't return itself sorted... that is, for 24, it'd return 1, 2, 12, 3, 8, 4, 6, and 24 instead of 1, 2, 3, 4, 6, 8, 12, and 24. I can't print it as a table, so it does need to be returned as a list. If it has to be sorted as a table first THEN turned into a list, I can deal with that. All that matters is the end result being the list.

The other detail is that I need to check if there are only two numbers in the list or more. If there are only two numbers, it's a prime (1 and the number). The current way I have it does not work. Is there a way to accomplish this? I appreciate all the help!

function get_all_factors(number)
  local factors = 1
  for possible_factor=2, math.sqrt(number), 1 do
    local remainder = number%possible_factor

    if remainder == 0 then
      local factor, factor_pair = possible_factor, number/possible_factor
      factors = factors .. ", " .. factor
      if factor ~= factor_pair then
        factors = factors .. ", " ..  factor_pair
      end
    end
  end

  factors = factors .. ", and " .. number
  return factors
end

local allfactors = get_all_factors(zs.param(1))
if zs.func.numitems(allfactors)==2 then
  return zs.param(1) .. " is prime."
else
  return zs.param(1) .. " is not prime, and its factors are: " .. allfactors
end
+3  A: 

If I understood your problem correctly I recommend splitting up your logic a bit. The idea would be first to create a table containing the fractions and then doing the sort and after that creating the string representation.

-- Creates a table containing all the factors for a number.
function createFactors(n)
    local factors = {}
    -- The for loop etc. would be here. If you find a factor then add
    -- it in the table.
    -- ...
    table.insert(factors, n)
    -- ...
    --# Once you've found all the factors just return the table.
    return factors
end

-- Lua offers a method for sorting tables called table.sort.
local factors = createFactors(139)
table.sort(factors)

-- There is a method for creating a string representation of a table
-- called table.concat, the first parameter is the table and the second
-- is the character that is used to delimit the values.
table.concat(factors, ", ")
ponzao
Did not know about table.concat. Thanks! What about counting the number of items?
Josh
Never mind! Found out about using #table! Thanks again!
Josh
No problem. Stack overflow doesn't seem to like Lua code with comments :(
ponzao
I've found that using <pre> for Lua code is clearer because of the code coloring issue. The built-in code coloring apparently assumes C-like languages and gets the colors all wrong with Lua.
RBerteig
@RBerteig, yeah it looks cleaner, thanks!
ponzao
+2  A: 

Nice ansewr from ponzao. To put the finishing touches on your result, here's a general-purpose routine for turning a list of strings in Lua into an English string, with "and", that represents the list:

function string.commafy(t, andword)
  andword = andword or 'and'
  local n = #t
  if n == 1 then
    return t[1]
  elseif n == 2 then
    return table.concat { t[1], ' ', andword, ' ', t[2] }
  else
    local last = t[n]
    t[n] = andword .. ' ' .. t[n]
    local answer = table.concat(t, ', ')
    t[n] = last
    return answer
  end
end

Instead of table.concat(factors, ', ') use string.commafy(factors).

Norman Ramsey
Awesome. This solves one of the things I wanted, too! :D Thanks!
Josh