General FizzBuzz: Difference between revisions
Content added Content deleted
(→{{header|Java}}: Added jFizzBuzz aka FizzBuzz Enterprise Edition.) |
(→{{header|Lua}}: Added version without modulo.) |
||
Line 1,162: | Line 1,162: | ||
genFizz(param) |
genFizz(param) |
||
</lang> |
</lang> |
||
'''One of the few solutions which do not use expensive modulo ''(think about the CPU!)''.''' |
|||
{{trans|Python}} |
|||
<lang Lua>local def_n = 100 |
|||
local def_mods = { |
|||
[3] = 'Fizz', |
|||
[5] = 'Buzz', |
|||
} |
|||
local function fizzbuzz(n, mods) |
|||
n = n or def_n |
|||
mods = mods or def_mods |
|||
local res = {} |
|||
for num, name in pairs(mods) do |
|||
for i = num, n, num do |
|||
res[i] = (res[i] or '') .. name |
|||
end |
|||
end |
|||
for i = 1, n do |
|||
res[i] = res[i] or i |
|||
end |
|||
return table.concat(res, '\n') |
|||
end |
|||
do |
|||
local n = tonumber(io.read()) -- number of lines, eg. 100 |
|||
local mods = {} |
|||
local n_mods = 0 |
|||
while n_mods ~= 3 do -- for reading until EOF, change 3 to -1 |
|||
local line = io.read() |
|||
if not line then break end |
|||
local s, e = line:find(' ') |
|||
local num = tonumber(line:sub(1, s-1)) |
|||
local name = line:sub(e+1) |
|||
mods[num] = name |
|||
n_mods = n_mods + 1 |
|||
end |
|||
print(fizzbuzz(n, mods)) |
|||
end |
|||
</lang> |
|||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
This solution has no input validation |
This solution has no input validation |