How to make my calculator more efficient?

I have four rocks. Game tells me three.

1 Like

May I see the code and the setup in the explorer?

image

2 Likes

You didn’t ask the AI nice enough.

1 Like

Do you have advice on making the AI even better?

1 Like

of course this doesn’t work, you need to capitalize it correctly.

local AI = ChatGPT
2 Likes

To calculate an addition, one may even recall its first natural method of calculating it: counting 1 by 1 (beware: this will only work with 2 positive integers)!
So here is a very intuitive way of adding 2 numbers num1 and num2 (each of these can represent any positive integer):

local function sum(num1, num2)
    if num2 == 0 then return num1
    else
        return sum(num1 + 1, num2 - 1)
    end
end

Basically, if we want to add 3 to 8, we would keep adding 1 until the 3 is exhausted, so the successive calculations would be (8,3) → (9,2) → (10,1) → (11,0). When we reach 0 with our num2, then it means we added everything! Therefore we can return num1 which has the value of the sum.
If you don’t understand something, please let me know!

W ragebait :clap: :clap: :clap:
You made all these guys wasye their time for nothing lol

3 Likes

im in pain having seen this :pray:

1 Like

Yeah but I want a negative integer to be added. What about that

1 Like

i havent seen all the replies but whats the fuss in doing num1 + num2 or get children in a folder like someone said

i never thought id see a post trolling in dev forum
first time

3 Likes

It’s called a substraction, and it can also be made intuitively! Consider the rework of my previous function to this:

local function sum(num1, num2)
    if num2 == 0 then return num1
    else
        if num2 < 0 then
            return sum(num1-1, num2+1)
        else
            return sum(num1 + 1, num2 - 1)
        end
    end
end

For negative numbers, we keep substracting to num1 until there is nothing left to substract from num2!

You need to create a lookup table… the if statements look very stinky and i don’t like them

You could use OOP and Metatables to create number objects, then use metatable __add to create addition and subtraction for each of them

This way you can learn a lot of new concepts which are usefull
Example:

local __Number = {}
__Number.__index = __Number

function __Number.new(Value: number)
    local self = setmetatable({}, __Number)
    self.Value = Value

    return self
end

function __Number:add(_Number)
    self.Value += _Number.Value
end

function __Number:subtract(_Number)
    self.Value -= _Number.Value
end

function __Number:getValue()
    return self.Value
end

function __Number:destroy()
    self.Value = nil
    setmetatable(self, nil) --/ Preventing memory leaks
end

This way you can add a lot more functionality to your numbers, example:

local __Number = require(script.__Number)
local a = __Number.new(5)
local b = __Number.new(2)

a:add(b)
print(a:getValue())

This methood is very scalable and allows you to perform action on numbers, not on variables

There is alternative, which is simple function

local function add(Number1, Number2)
    return Number1 + Number2
end

I know how to do math and it’s called a subtraction.

Why would I return num1 and num2… I want the two combined. Man

1 Like

But i want to use numbers not __numbers… we need to be confident with what we name our objects. Putting two underscores in English could be a sign of trying to find the words before we say something.

1 Like

Because 1 + 2 = 3. I want num1 and num2 to do math stuff

1 Like

Do you only want it do math? If really don’t want to do them in a folder you can just don for loop

function CountRocks()
local Rockamout = 0
for i, obj in workspace:GetChildren() do
If obj.Name == "Rock" then -- change to the proper name
Rockamout += 1
end
end
return Rockamout
end

I don’t want my program to end I want it to give me the answers.

1 Like