验证中...
私信发送成功
语言: Lua
分类: 编程语言基础
最后更新于 2017-10-13 02:38
factorize.lua
原始数据 复制代码
#!/usr/bin/env lua
local function factorize(num)
local result = num .. '='
local factors = {}
local temp
while num > 1 do
temp = num
for i = 2, math.ceil(math.sqrt(num)) do
if num % i == 0 then
factors[i] = (factors[i] or 0) + 1
num = num / i
break
end
end
if num == temp then
factors[num] = (factors[num] or 0) + 1
break
end
end
table.sort(factors)
for k, v in pairs(factors) do
if v == 1 then
result = result .. k .. '*'
else
result = result .. k .. '^' .. v .. '*'
end
end
return string.sub(result, 1, -2)
end
local n
for _, v in ipairs(arg) do
n = tonumber(v, 10)
if not n or n < 2 or n % 1 ~= 0 then
print(string.format('cannot factorize: %s!', v))
else
print(factorize(n))
end
end

评论列表( 0 )

你可以在登录后,对此项目发表评论

4_float_left_people 4_float_left_close