help
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Re: help
Hi.
If I understand you, you might have "22,99,33,66,44,77,55", all of which are multiples of "11". And you want to know which is the lowest?
Read about the "sort container" command in the dictionary. You would sort the above list "numeric" by items, and extract the first item.
Craig Newman
If I understand you, you might have "22,99,33,66,44,77,55", all of which are multiples of "11". And you want to know which is the lowest?
Read about the "sort container" command in the dictionary. You would sort the above list "numeric" by items, and extract the first item.
Craig Newman
Re: help
So you really want a LCM calculator.
This is simple and straightforward to do in LC, but "tedious". You might find the product of the entire list, then all its factors, and then see if each number in the list mod each factor gives a "0". The lowest factor would be your LCM.
Hermann, are you there?
Craig
This is simple and straightforward to do in LC, but "tedious". You might find the product of the entire list, then all its factors, and then see if each number in the list mod each factor gives a "0". The lowest factor would be your LCM.
Hermann, are you there?
Craig
Last edited by dunbarx on Thu Dec 13, 2018 12:39 am, edited 1 time in total.
Re: [help] LCM and GCD of a list of integers
[Notation. LCM=Least Common Multiple, GCD=Greatest Common Divisor]
Craig.
You describe an algorithm that is better for understanding the problem.
But the most efficient way to compute the LCM of TWO integers a and b is to use the formula
(*) abs(a*b)=LCM(a,b)*GCD(a,b)
because the computation of GCD is so simple:For a list x1,x2,...,xn of n integers we loop to get both GCD and LCM
using LCM(a,b,c)=LCM(LCM(a,b),c) and GCD(a,b,c)=GCD(GCD(a,b),c).
Examples
1,2,8,3 --> GCD=1, LCM=24 [Note: Formula (*) above is only valid for TWO integers.]
12,10 --> GCD=2, LCM=60
Craig.
You describe an algorithm that is better for understanding the problem.
But the most efficient way to compute the LCM of TWO integers a and b is to use the formula
(*) abs(a*b)=LCM(a,b)*GCD(a,b)
because the computation of GCD is so simple:
Code: Select all
-- Euklid's algorithm
-- Greatest Common Divisor of two integers x and y
function GCD x,y
if x is 0 then return abs(y)
if y is 0 then return abs(x)
repeat while y <> 0
put x mod y into t
put y into x
put t into y
end repeat
return abs(x)
end GCD
using LCM(a,b,c)=LCM(LCM(a,b),c) and GCD(a,b,c)=GCD(GCD(a,b),c).
Code: Select all
on mouseUp
put fld "IN" into L -- L = one line of comma separated integers
put item 1 of L into z1
put item 1 of L into z2
repeat with i=2 to the num of items of L
put GCD(z1,item i of L) into z1
put z2*item i of L/GCD(z2,item i of L) into z2
end repeat
put z1 into fld "GCD" -- the GCD of L (Greatest Common Divisor)
put z2 into fld "LCM" -- the LCM of L (Least Common Multiple)
end mouseUp
1,2,8,3 --> GCD=1, LCM=24 [Note: Formula (*) above is only valid for TWO integers.]
12,10 --> GCD=2, LCM=60
shiftLock happens
Re: help
Craig.
Yes. But he has it for any finite number of integers. All we need is GCD(x,y) for two integers (see above).
Example:
LCMofList("1,2,8,3") -- returns 24
Yes. But he has it for any finite number of integers. All we need is GCD(x,y) for two integers (see above).
Code: Select all
-- L = one line of comma separated integers
function LCMofList L
put item 1 of L into z2
repeat with i=2 to the num of items of L
put z2*item i of L/GCD(z2,item i of L) into z2
end repeat
return z2
end LCMofList
LCMofList("1,2,8,3") -- returns 24
shiftLock happens