I recently learned from my professor a new way to improve your advanced number system. It is called object oriented programming
Object-oriented programming (OOP) is a programming paradigm that revolves around the concept of objects, which are instances of classes that can contain data and code to manipulate that data. One of the key principles of OOP is modularity, which refers to the practice of breaking down a program into smaller, more manageable pieces called modules or classes.
Modularity in OOP offers a number of benefits, including:
Reusability: Modularity allows developers to reuse code by creating classes that can be used in multiple parts of a program or even in different programs altogether. This can save time and effort by avoiding the need to rewrite the same code multiple times.
Encapsulation: In OOP, each module or class encapsulates its own data and methods, meaning that the inner workings of a class are hidden from other parts of the program. This helps prevent accidental modification of data and ensures that changes to a class do not affect other parts of the program.
Maintainability: By breaking a program into smaller modules, developers can more easily understand and modify the code. This makes it easier to fix bugs, add new features, or make updates to the program without having to modify large portions of code.
Scalability: Modularity makes it easier to scale a program by adding new modules or classes as needed. This allows developers to extend the functionality of a program without having to redesign the entire system.
It provides a clear, easy to use interface and automates your code by generating it for you!
--// Removes some bugs \\
--!nocheck
--!nolint
--!optimize 0
--// VARIABLES \\
NumberCalculator = {}
NumberCalculator.__index = NumberCalculator
--// FUNCTIONS \\
NumberCalculator.new = function(n)
self = {}
self.n = n
c = ("number = %d "):format(n)
c = c .. ("if number == 1 then print(\"uneven\") "):format(n)
for i = 2, n do
c = c .. (`elseif number == %d then print({if i % 2 == 0 then "\"even\"" else "\"uneven\""}) `):format(i)
end
c = c .. " else print(\"Unsupported number\") end"
self.calculate = loadstring(c)
return self
end
return NumberCalculator
Hello, I think this function will work well for your purpose.
local Number = {};
setmetatable(Number, {
__call = function(T, ...)
return T:Start(...);
end
});
function Number:Start(i : Integer)
local InstancedNumber = {};
InstancedNumber.Number = i;
setmetatable(InstancedNumber, {
__index = Number
});
return InstancedNumber;
end
function Number:IsEven()
return bit32.rshift(self.Number, 1) == 0;
end
--// check if a number is even
Number(3):IsEven() --// returns false
No it’s not.
% is taking the remainder operator. You devide number by 2, so if you will 3%2 (3/2) it gives 1 as remainder, and if 4%2 it gives 0 since 4 devides by 2 perfectly. If number % 2 doesnt get any remainder and it devides perfectly then number is even, else which means that there IS remainder its odd. I hope i helped!
This has changed my life for the better and has allowed me walk my meatless dogs, thanks! I’ve just replaced my smooth brain IsEven function in my project for this, I’m mindblown people are giving AAA code for free.
I also can’t wait for the 10 million update like @CryBlanka and maybe a Gigaparsec update soon?
Using a modulo operator like this, is how this is done in assembler.
You’re never going to get more optimized than that.
I think some of you are just trolling …
neat trick for seeing if something is divisible by three
function IsDivisibleByThree(number: number)
local str = tostring(number)
if string.find(str, ".") then
return false
else
str = str:gsub("-", "")
local antiLag = 0
while str:len() > 1 do
local tab = str:split("")
local total = 0
for _, i in tab do
total += tonumber(i)
end
antiLag += 1
if antiLag%25 == 0 then
task.wait()
end
str = tostring(total)
end
if str == "9" or str == "6" or str == "3" then
return true
else
return false
end
end
end
you could also do this, however it is very complicated.
function IsDivisibleByThree(number: number)
if number%3 == 0 then
return true
else
return false
end
end
too complicated, not what any of us are looking for.
we, as developers, prefer simple, compact, optimized and efficient code. @LotoNika’s project includes that, and what you offer is:
too complicated (not simple),
long,
unoptimized,
and inefficient.
Its not, though. In most C++ compilers, modulus is turned into bit shifting whenever possible. And if that’s not possible (not a power of 2) it then uses multiplication (like how the compiler does with division) and uses more bit shifting on that to get the remainder. I don’t think any CPU has a modulus instruction, when many don’t even have division.