Help with handwritten advanced number system

The 100 million version works well in my game. Can’t wait for the 1 billion update.

LOL I see what you’re trying to do but that’s extremely unoptimized and just calling the function once will probably crash the whole server. Instead, you could easily detect even numbers by using math.round(), then divided whatever you have by 2, and then check if the remainder is < 1 (less than = odd). This is the easiest solution and it wont turn your device into a oven :sob:

I appreciate your dedication in learning how to script and you’re doing well so far because functionally this does work, its just very very very inefficient especially when you can do the same thing in like 3 lines

edit: woops, nevermind lol that wont work either… I don’t know what i was thinking but you could do something like this that uses the same principle

function is_even(number)
    return math.floor(number / 2) * 2 == number
end

if is_even(number) then
    print(number .. " is even")
else
    print(number .. " is odd")
end
1 Like

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
1 Like

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.

1 Like