How to Detect Whether a Number is an Integer or not?

So, I want to make a greatest common factor calculator, but I don’t know how to see if a number’s an integer.

Script:

function GCF(Num)
	for i = 1, Num, 1 do
		local PossibleFactor = Num/i
		if  then -- Check if Num is an integer
			print(PossibleFactor)
		end
	end
end

GCF(40)
if N % 1 == 0 then
    --number is an integer above 0
end
5 Likes

It doesn’t work, it still prints decimals.

do you want the if statement to run if it is a decimal?

Do % 2 instead of % 1, it will always return 1 if it’s an integer.

1 Like

No, I want it to print out all of the integers.

Now it doesn’t print out anything.

Wait no I lied, it returns 1 if odd and 0 if even, only if it’s an integer

% will divid and then get the remainder

every integer is dividable by 1 so % 1 works
% 2 will only work for even numbers

1 Like

Can I see the print line?

It also works for odd numbers, but it will return 1 as a remainder

yea which isn’t 0, that was my point :sweat_smile:

I had == 0 in my if statement so yea

What do you want to see printed?

print(number % 2)

You can just check if it’s an integer like this:

if number % 2  == 1 or number % 2 == 0 then

end

Use modulus with 1, not 2, as @D0RYU said in the first reply to this topic.

This modification is tested and works in my studio:

function GCF(Num)
	for i = 1, Num, 1 do
		local PossibleFactor = Num/i
		if PossibleFactor % 1 == 0 then -- Check if Num is an integer
			print(PossibleFactor)
		end
	end
end

GCF(40)

It still prints out decimals in the output.

@NeonTaco135 yeah I forgot to check with 1, @D0RYU is right, it will just return 0 if it’s an integer. It will return a decimal if it’s not

that is unnecessary code
the first if statement I brought up was shorter and does the same thing as yours