Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to simplify numbers like 36 into three 10s and one 6?

Asked by 5 years ago
Edited 5 years ago

This question has been solved by the original poster.

Hi, I have been trying to turn stuff like 30 into three 10s or 45 into four 10s and one 5.

I have tried with the code below which doing numbers like 30 work, but numbers like 15 only print the 10 out.

function turnNumberToMoney(n)
    if n < 10 then
        return n
    end
    local nums = {}
    repeat wait()
        if n ~= 10 then
            if n > 10 then
                table.insert(nums,10)
                n = n - 10
            else
                table.insert(nums,n)
                break
            end
        else
            if n == 10 then
                table.insert(nums,10)
            end
            if n == 5 then
                table.insert(nums,5)
            end
            break
        end
    until n < 10
    return nums
end

local num = turnNumberToMoney(15)

for i,v in pairs(num) do
    print(v)
end
0
This is a pretty standard interview question, so plenty of stuff will come up if you Google it: https://www.geeksforgeeks.org/find-minimum-number-of-coins-that-make-a-change/ fredfishy 833 — 5y
0
in LUA code? the8bitdude11 358 — 5y
0
whenever i look it up i get stuff about decimals the8bitdude11 358 — 5y
0
math.floor(36/10) + math.floor(36 - (36/10) /5)? brokenVectors 525 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

I got it working for numbers under 1000

function convert10(num)
    local str = tostring(num)
    local n1,n2 = str:sub(1,1),str:sub(2,2)
    local tab = {}

    for i = 1,tonumber(n1) do
        table.insert(tab,10)
    end
    table.insert(tab,n2)
    return tab
end

function convert100(str)
    local num = tostring(str)
    local n1,n2,n3 = num:sub(1,1),num:sub(2,2),num:sub(3,3)
    local tab = {}
    for i = 1,tonumber(n1.."0") do
        table.insert(tab,10)
    end
    for i = 1,tonumber(n2) do
        table.insert(tab,10)
    end
    table.insert(tab,n3)
    return tab
end
Ad

Answer this question