Help with handwritten advanced number system

local function isEven(f, n)
	if not n or type(n) ~= "number" then
		return
	end
	
	local n_str = tostring(n)

	if n_str:find("%.") then
		return
	end
	
	local n_tab = {}
	n_str:gsub(".", function(c)
		table.insert(n_tab, c)
	end)
	
	local maxn = #n_tab
	if not maxn then
		return
	end
	
	local val = n_tab[maxn]
	
	if val == "2" or val == "4" or val == "6" or val == "8" or val == "0" then
		f()
	end
end

or maybe just use

function isEven(n)
     if type(n) ~= "number" then return false end
     return math.fmod(n, 2) == 0
end

this is very inefficient compared to the op’s code! you should never use % as it isn’t optimized

1 Like

Actually true, modulus is the slowest operator. Since it involves a division + more.

If Luau compiles the if statement here into a jump table, it might actually be faster than using modulus.

You have managed to fill more memory than an average unoptimized triple-A game. I commend you for that.

Can you create a Binary (base2) to Base10 (normal numbers)? For the fun of it add support for hexadecimal. (Base16)

I am way too lazy to continue this conversion by hand.

I think this is more efficient code, and much more compact. Modulus is extremely unoptimized. I found this method faster and less memory stressing than the if-then chain going over x*2+10 lines. (x is how many numbers are supported)

function isEven(number: number): boolean
	 return not tostring(tonumber(number)/2):find(".5")
end 

print(isEven(2024)) --> true
print(isEven(2023))  --> false

I love the snarkiness of this comment.

1 Like

believe it or not, fmod is much slower than ‘%’ operator
i did the benchmarking, it’s constantly slower

it’s made for people on Lua, which doesn’t have ‘%’

Why are yoiu all so rude towards me, all I do is I’m trying to work on this advanced alghoritm and everyone just gives me a headache.

1 Like

this is clearly the most efficent way to check if a number is odd or even. it is very understandable and very easy to add numbers.

i suggest you put a task.wait() before the prints

this ensures your code won’t throttle and won’t run out of memory

remember that running out of memory leads to crashes! :smiley:

1 Like

O idk what’s that but sounds nice, will think about it, thanks!

1 Like

also do this to make your code run faster

local lol={}

for i=1,99999 do
       table.insert(lol,table)
       table.insert(lol,getfenv)
       table.insert(lol,i)
end

fameboy

there is NO way this wasn’t made as a joke

(btw this isn’t really an advanced number system as all it does is just calculate if a number is even or not… In a horribly unoptimised way)

pls dont run this on a localscript or script, im pretty sure the server/computer will Explode

2 Likes

let me simplify this so you can write it yourself:

the definition of an “Even” number is a number that can be divided by 2 successfully without decimals.

the definition of an “Odd” number is a number that will have decimals if divided by 2.

example: 8 is an Even number
image

example: 9 is an Odd number
image

you can make a… simple formula/function by just dividing the number by 2 then rounding it, there is probably better and more optimised ways but this is the simplest i know which will be good for… this kind of case

so… use math.round() to round the number and if its the same as the original number, its even, else its odd

function worldsSimplestEvenOddFormula(my_number)
	
	divided_number = my_number / 2
	rounded_number = math.round(divided_number)

	if divided_number == rounded_number then
		return true -- Even
	else
		return false -- Odd
	end
end

if you cant understand this uhhh Yeah its so over

(i am 99.999% sure this was topic was made as a joke, op has ignored the many other methods above me)

2 Likes

OP code is more optimized than this based on my visual speculation

1 Like

It’s not a joke, I’m try my best but everyone sending is complicated code which i not understand…

1 Like

This gave me inspiration to start creating my own advanced number algorithm that checks if a number is positive or negative and it currently supports 7 numbers! Since OP seems very experienced in this field i wanted to ask: how can i improve my code?

 -- very advanced as you can see
local function IsPositive(x: number): boolean
	if x == 3 then
		return true
	elseif x == 2 then
		return true
	elseif x == 1 then
		return true
	elseif x == 0 then
		return true
	elseif x == -1 then
		return false
	elseif x == -2 then
		return false
	elseif x == -3 then
		return false
	else
		error("Oh no!")
	end
end
1 Like

0 is not positive, by the way. If your function name was IsNonNegative, then returning true for 0 would be correct.

1 Like

Your even or uneven number checker is way too unoptimized
Here I wrote a way simplier way to check if the number is even or uneven.

-- Your number
local number = 100

-- Metatable to extend the functionality of the number
NumberMeta = {}
NumberMeta.__index = NumberMeta

-- Constructor for creating a new number object
function NumberMeta:new(value)
	local instance = setmetatable({}, self)
	instance.value = value
	return instance
end

-- Function to check if the number is even
function NumberMeta:is_even()
	return self:_check_evenness(self.value)
end

-- Private function to check evenness recursively
function NumberMeta:_check_evenness(num)
	if num == 0 then
		return true
	elseif num == 1 then
		return false
	else
		return self:_reduce_number(num)
	end
end

-- Private function to reduce the number and check if it is even
function NumberMeta:_reduce_number(num)
	local half_num = self:_half(num)
	if self:_double(half_num) == num then
		return true
	else
		return false
	end
end

-- Private function to calculate half of the number iteratively
function NumberMeta:_half(num)
	local half = 0
	local count = 0
	while count < num do
		count = count + 2
		if count <= num then
			half = half + 1
		end
	end
	return half
end

-- Private function to double the number iteratively
function NumberMeta:_double(num)
	local doubled = 0
	for i = 1, num do
		doubled = doubled + 2
	end
	return doubled
end

-- Main function to drive the program
function main()
	local number_obj = NumberMeta:new(number)
	if number_obj:is_even() then
		print(string.format("The number %d is even.", number))
	else
		print(string.format("The number %d is odd.", number))
	end
end

-- Execute the main function
main()

Thank me later.

you’ve only created one devforum topic (this one) and “somehow” reside in the capital of north korea…

this is most likely a ragebait account, so im not continuing from here.
if you really are trying, im glad so keep doing that. But personally i think with the amount of solutions here and literal code examples there is plenty of ways of you to “not fry your computer alive”

good luck

2 Likes

plus the account was created the same day as this post and will slap the post link wherever it can (oh god I’m replying to the topic, rip my notifications)

1 Like