Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

4 comments

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/

0

in LUA code?

0

whenever i look it up i get stuff about decimals

0

math.floor(36/10) + math.floor(36 - (36/10) /5)?

1 answer

✓ Accepted answer

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

This discussion is preserved as it was. New answers, comments, and votes are closed.