So I am making a script that tells me what number is a prime number.
local dividend= 1 local divisor= 1 function divide() local result= dividend/divisor --if result is not a decimal then --if result is not 1 or "dividend" then print(dividend.."is not a prime number") else print(dividend.."is a prime number") dividend= dividend + 1 divisor= 1 end end divisor= divisor + 1 end while true do divide() wait() end
In line 7, I don't know how to find if "result" has a decimal, as per the question says. In line 8, as soon as I type "not", it gets a red line under it.
Please note that I put the end and else just for convenience so please don't tell me that's the one incorrect.
You can do this two different ways. Using the function modf()
from math, we can devise two results from the number given: It's integral, and fractional component. If we retrieve a fractional component greater than zero, our number contains decimals. To use this directly in a conditional, we can use select()
in conjunction with it to compare the second result.
local Pi = 3.14 if (select(2,math.modf(Pi)) > 0) then print("Is a float!") end
We can also use string patterns if you wish to match a dot character and a digit (which is a result of a decimal:
local Pi = 3.14 if (tostring(Pi):match("%p%d+") then print("Is a float!") end