Help needed calculating LCM

Does anybody know how I could make a function to get the LCM of 2 numbers (or more)? I’ve started but I’m a little bit stuck.

Here’s what I have so far:

local function GCF(n1, n2)
	local gcd = 1
	
	if n1 > n2 then
		n1 = n1 + n2
		n2 = n1 - n2
		n1 = n1 - n2
	elseif (n2 == math.round(n2 / n1)) * n1 then
		gcd = n1
	else
		for i = math.round(n1 / 2) 
	end
end

local function LCM(n1, n2)
	
end

I haven’t checked your GCF function for correctness, but the LCM can be found as

|ab| / GCD(a,b).

So assuming your GCF works you can do:

    return math.abs(n1,n2)/GCF(n1,n2)
1 Like

Do you know how I could get GFC? That’s the part I got stuck on

I’d recommend trying Euclid’s Algorithm.

You can find a calculator with steps for that here:

https://www.calculatorsoup.com/calculators/math/gcf-euclids-algorithm.php

Remember you can use modular division with % to get the remainder.