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 6 years ago
Edited 6 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.

01function turnNumberToMoney(n)
02    if n < 10 then
03        return n
04    end
05    local nums = {}
06    repeat wait()
07        if n ~= 10 then
08            if n > 10 then
09                table.insert(nums,10)
10                n = n - 10
11            else
12                table.insert(nums,n)
13                break
14            end
15        else
View all 32 lines...
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 — 6y
0
in LUA code? the8bitdude11 358 — 6y
0
whenever i look it up i get stuff about decimals the8bitdude11 358 — 6y
0
math.floor(36/10) + math.floor(36 - (36/10) /5)? brokenVectors 525 — 6y

1 answer

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

I got it working for numbers under 1000

01function convert10(num)
02    local str = tostring(num)
03    local n1,n2 = str:sub(1,1),str:sub(2,2)
04    local tab = {}
05 
06    for i = 1,tonumber(n1) do
07        table.insert(tab,10)
08    end
09    table.insert(tab,n2)
10    return tab
11end
12 
13function convert100(str)
14    local num = tostring(str)
15    local n1,n2,n3 = num:sub(1,1),num:sub(2,2),num:sub(3,3)
View all 25 lines...
Ad

Answer this question