How to make my calculator more efficient?

the funniest part of this is that there’s no var inputs

actually there is. using __call metamethod it lets the returned metatable be called as a function and gets the args from a tuple.

“help with my flower” ahh post

You can make this automatic using loadstring!

You just write some code, that generates your if-statements!

function writeCodeForMe(n)
	local code = ""
	
	for i = 0, n do
		for j = 0, n do
			code ..= `if num1 == {i} and num2 == {j} then return {i + j} `
			if not (i == n and j == n) then
				code ..= "else"
			end
		end
	end

	code ..= " end"
	return code
end

This will return our code as a string, ready for us to use. We then just need to use loadstring!
Although we also need to make sure that num1 and num2 are part of the function environment, so we do a bit of trickery with the function parameters.

The final script looks like this:

local maxNumber = 5

function writeCodeForMe(n)
	local code = ""
	
	for i = 0, n do
		for j = 0, n do
			code ..= `if num1 == {i} and num2 == {j} then return {i + j} `
			if not (i == n and j == n) then
				code ..= "else"
			end
		end
	end

	code ..= " end"
	return code
end

function addition(a, b)
	num1 = a
	num2 = b
	return loadstring(writeCodeForMe(maxNumber))()
end

And this works! Now we can just change the maxNumber variable, and it will automatically make new if-.statements for us!

try restarting your computer. if that doesnt work then plug it in and out

1 Like

no, it’s more like this post

Does it support trigonometry yet? I’m currently working on a trig class, and if I could practice trig while being locked into that Roblox grind that would be amazing.

Clever, I am starting to believe in myself again.

He is trying to do the possible, I, on the otherhand, seek out to achieve the impossible.

No, this is BqttlesTheOnly. Put some respect on my name before moving your deceitful lips like a clown.

1 Like

I do not accept this kind of disrespect.
You have no rights to talk on this category, since you can even script properly, so instead of wasting peoples time, go touch some grass or learn programming.
Seriously, don’t you have something better to do with your life, like helping people on the Forum?

My brain was hurting just reading this whole thing.
This is the craziest troll, how does someone have so much time and no life to the point they create a post and consistently keep on talking and trolling even when everyone knows it’s a troll.

You’re crazy, but so am I.
i think, you can use that to improve your god script, I know a much better way.

local function to_unary(n)
	local unary = {}
	for i = 1, n do
		unary[#unary + 1] = 1
	end
	return unary
end

local function from_unary(unary)
	local count = 0
	for i, _ in ipairs(unary) do
		count = count + 1
	end
	return count
end

local function add_no_operators(unary_a, unary_b)
	local result = {}
	for i, _ in ipairs(unary_a) do
		result[#result + 1] = 1
	end
	for i, _ in ipairs(unary_b) do
		result[#result + 1] = 1
	end
	return result
end

local function subtract_no_operators(unary_a, unary_b)
	local result = {}
	local a_index = 1
	local b_index = 1

	while a_index <= #unary_a and b_index <= #unary_b do
		if unary_a[a_index] == 1 and unary_b[b_index] == 1 then
			a_index = a_index + 1
			b_index = b_index + 1
		else
			result[#result + 1] = unary_a[a_index]
			a_index = a_index + 1
		end
	end

	for i = a_index, #unary_a do
		result[#result + 1] = unary_a[i]
	end

	return result
end


local function multiply_no_operators(unary_a, unary_b)
	local result = to_unary(0)

	for i, _ in ipairs(unary_b) do
		result = add_no_operators(result, unary_a)
	end

	return result
end


local function divide_no_operators(unary_a, unary_b)
	local quotient = to_unary(0)
	local remainder = unary_a

	while #remainder >= #unary_b do
		remainder = subtract_no_operators(remainder, unary_b)
		quotient = add_no_operators(quotient, to_unary(1))
	end

	return quotient, remainder
end

or u can do smthg like this

local Zero = {}
local One = {}
local Two = {}
local Three = {}
local Four = {}
local Five = {}

function Zero.isZero(self) return true end
function Zero.increment(self) return One end
function Zero.decrement(self) return self end
function Zero.toString(self) return "Zero" end

function One.isZero(self) return false end
function One.increment(self) return Two end
function One.decrement(self) return Zero end
function One.toString(self) return "One" end

function Two.isZero(self) return false end
function Two.increment(self) return Three end
function Two.decrement(self) return One end
function Two.toString(self) return "Two" end

function Three.isZero(self) return false end
function Three.increment(self) return Three end
function Three.decrement(self) return Two end
function Three.toString(self) return "Three" end

function Four.isZero(self) return false end
function Four.increment(self) return Four end
function Four.decrement(self) return Two end
function Four.toString(self) return "Three" end

function Five.isZero(self) return false end
function Five.increment(self) return Five end
function Five.decrement(self) return Two end
function Five.toString(self) return "Three" end


local function add(num1, num2)
	if num2.isZero(num2) then
		return num1
	else
		return add(num1.increment(num1), num2.decrement(num2))
	end
end


local function multiply(num1, num2)
	if num2.isZero(num2) then
		return Zero
	elseif num2 == One then
		return num1
	else
		return add(num1, multiply(num1, num2.decrement(num2)))
	end
end


local a = Five
local b = Three

local sum = add(a, b) --Ошибка на етой строке
print(sum:toString())

local product = multiply(a, Two)
print(product:toString())

I am here to learn how to program. How can you be so hateful to a youthful programmer trying to learn how to code…

Genius. We are embarking on a new era of humanity. I do love this movement, brother.

I am not trolling. I am simply trying to learn how the wonderful world of Roblox programming works!

Fun fact: I bench 255 pounds.

Wow u can finally bench ur own weight.
Fun fact: We don’t care

1 Like

Frankly, I think we should organize an event where the people calling you a troll and you can box each other.
Maybe some beatboxing too?

While I am not a troll, I would absolutely love to have an education rap battle regarding ROBLOX programming. This would be an extravagant event! We can discuss the ways to create a calculator.

1 Like

My bodyweight is 175 lbs actually, but that was quite the zinger! Perhaps you can be my coding rap battle opponent with that wit of yours?