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
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
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
example: 9 is an Odd number
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)
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
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()
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”
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)