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)
D0RYU
(nici)
May 24, 2021, 3:04pm
#2
if N % 1 == 0 then
--number is an integer above 0
end
5 Likes
It doesn’t work, it still prints decimals.
D0RYU
(nici)
May 24, 2021, 3:06pm
#4
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
D0RYU
(nici)
May 24, 2021, 3:08pm
#9
% 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
D0RYU
(nici)
May 24, 2021, 3:09pm
#12
yea which isn’t 0, that was my point
D0RYU
(nici)
May 24, 2021, 3:09pm
#13
I had == 0 in my if statement so yea
What do you want to see printed?
You can just check if it’s an integer like this:
if number % 2 == 1 or number % 2 == 0 then
end
blokav
(blokav)
May 24, 2021, 3:12pm
#17
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
D0RYU
(nici)
May 24, 2021, 3:14pm
#20
that is unnecessary code
the first if statement I brought up was shorter and does the same thing as yours