If you do print(|-3|)
would it print 3? I tried this once and it didn't work, but I still think there is a way to find absolute value. I want it to be simple and not like:
local number = {-4, 4, 5, -98} for _, num in pairs(number) do if num > 0 then num = num*-1 print(num) else print(num) end
I also want to know if you can do factorial in lua: print(3!)
should print 6, unless I've done it wrong(that would be embarrassing.) This is how you could do it but it's too long.
local number = {3, 5, 7} for _,num in pairs(number) do if num == 0 then print(1) elseif num < 0 then repeat bleh = -1 num = num*num+bleh bleh = bleh-1 until num-bleh = 1 print(num) elseif num > 0 then repeat bleh = 1 num = num*num-bleh bleh = bleh+1 until num+bleh = -1 print(num) end end end
Thanks in advance! BTW: I know these scripts don't work, I made them in under a min. Expect bugs and me spelling things wrong.
The abs
function of the math library does absolute values. As for factorials, you have to program that yourself:
function factorial(num) num = math.abs(num) --Factorial only works for positive numbers. num = math.floor(num + .5) --Factorial only works for integers. local result = num while num > 0 do num = num - 1 result = result*num end return result end