help

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
B1o0b1
Posts: 3
Joined: Tue Dec 11, 2018 5:21 pm

help

Post by B1o0b1 » Wed Dec 12, 2018 3:46 pm

I would like to know if anyone can help me to have a script to know who is the lowest common multiple among a set of numbers that are the multiple of the same number. thank you

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10321
Joined: Wed May 06, 2009 2:28 pm

Re: help

Post by dunbarx » Wed Dec 12, 2018 3:59 pm

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

B1o0b1
Posts: 3
Joined: Tue Dec 11, 2018 5:21 pm

Re: help

Post by B1o0b1 » Wed Dec 12, 2018 4:21 pm

Hi,
thank's, in fact I would like to know among those numbers which is the lowest common multiple (LCM) of others.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10321
Joined: Wed May 06, 2009 2:28 pm

Re: help

Post by dunbarx » Wed Dec 12, 2018 5:22 pm

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
Last edited by dunbarx on Thu Dec 13, 2018 12:39 am, edited 1 time in total.

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: [help] LCM and GCD of a list of integers

Post by [-hh] » Wed Dec 12, 2018 10:54 pm

[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:

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

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).

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
Examples
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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10321
Joined: Wed May 06, 2009 2:28 pm

Re: help

Post by dunbarx » Thu Dec 13, 2018 12:41 am

Ah, Hermann is here.

I think the OP is talking about more than two.

Craig

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: help

Post by [-hh] » Thu Dec 13, 2018 12:52 pm

Craig.

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
Example:
LCMofList("1,2,8,3") -- returns 24
shiftLock happens

B1o0b1
Posts: 3
Joined: Tue Dec 11, 2018 5:21 pm

Re: help

Post by B1o0b1 » Thu Dec 13, 2018 5:31 pm

hi Guys, I got it, Thank you so much.

Post Reply