Help with handwritten advanced number system

Hello, LotoNika.

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

--// MODULES \\

NumberCalculator = require(script.NumberCalculator)

--// VARIABLES \\

NumberCalculatorFactorySingleton = {}
NumberCalculatorFactorySingleton.__index = NumberCalculatorFactorySingleton

--// FUNCTIONS \\

NumberCalculatorFactorySingleton.new = function()
	self = {}
	setmetatable(self, NumberCalculatorFactorySingleton)
	return self
end

NumberCalculatorFactorySingleton.constructCalculator = function(self, n)
	self = NumberCalculator.new(n)
	return self
end

return NumberCalculatorFactorySingleton.new()
--// 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

Example usage:

NumberCalculatorFactorySingleton = require(script.Parent.NumberCalculatorFactorySingleton)
myCalculator = NumberCalculatorFactorySingleton:constructCalculator(11)
myCalculator.calculate()

image

No cc, no human verification 100% working 2024 best method free no charge

isOdd would return odd if for any decimal as well as odd number, so you need to check if %2==0.5

this would also would return odd if a decimal was used, a number not being even does not mean it is odd

mhh this seems optimized, keep it up your 900000 closer!

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
2 Likes

This function seems very complicated and not very efficient, not even a SINGLE else if statement… I believe OP is seeking much more efficient code.

3 Likes

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!

1 Like

this has to be a joke… please no way you wrote al lthis

Can’t believe that some people in this post don’t understand that this is satire :skull:
(OP’s solution is extremely efficient btw)

it would be even better if instead of having elseif statements, it would all be seperate if statements

1 Like

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?

1 Like

this code is extremely unoptimized. OP’s method is way better. some of you guys just dont like change.

2 Likes

i’ve loved this script, and have used it in all of my projects! could you please make one to tell if a number is divisible by three? thanks!

1 Like
function isOdd(number)
	return number %2~=0
end

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.

1 Like

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.

no, im not trolling… that’s actually extremely inefficient because the modulo operator has to use 2 tasks.

1 Like

holy yap its not that serious. just accept this person’s advanced written number system.

6 Likes

WHY WONT MY 1.0000000000000000000000000000000000000000000000000000000000000001 WORK11/!?!?!? /j
Seriously… This is chaos.