[Gambas-user] Least Common Multiple

Peter Moers peter.moers at ...626...
Tue Apr 11 17:01:29 CEST 2006


thanks for your help, I've got it working now.

2006/4/11, Timothy Marshal-Nichols <timothy.marshal-nichols at ...247...>:
>
>
> Why limit yourself to 3 numbers. Here is a function that takes a integer
> array:
>
> PUBLIC SUB Main()
>   DIM numbers AS Integer[3]
>   ' Get numbers
>   PRINT "Input three numbers: "
>   INPUT numbers[0]
>   INPUT numbers[1]
>   INPUT numbers[2]
>   PRINT "Least common multiple is " & LeastCommonMultiple(numbers)
> END
>
> PRIVATE FUNCTION LeastCommonMultiple(Numbers AS Integer[]) AS Integer
>   DIM i AS Integer
>   DIM loopNumber AS Integer
>   ' If 0 is in the number array then the least common multiple is 0
>   IF Numbers.Find(0) <> -1 THEN RETURN 0
>   ' Find LCM
>   loopNumber = 0
>   WHILE TRUE
>     ' Add the max integer in the array to loop variable
>     loopNumber += Numbers[Numbers.Max]
>     ' Check if this is LCM
>     FOR i = 0 TO Numbers.Count - 1
>       IF (loopNumber MOD Numbers[i]) <> 0 THEN
>         ' Not LCM. So break out of for loop to
>         ' try next value
>         BREAK
>       ELSE IF i = Numbers.Count - 1 THEN
>         ' loopNumber MOD Numbers[i] is 0 for all integers
>         ' in the array. So this is our LCM
>         RETURN loopNumber
>       END IF
>     NEXT
>   WEND
> END
>
>
> Thanks
>
> 8-{)} Timothy Marshal-Nichols
> <mailto: timothy.marshal-nichols at ...247...>
>
>
> -----Original Message-----
> From: gambas-user-admin at lists.sourceforge.net
> [mailto:gambas-user-admin at lists.sourceforge.net]On Behalf
> Of Allen Murphy
> Sent: Tuesday, 11 April 2006 02:20
> To: gambas-user at lists.sourceforge.net
> Subject: Re: [Gambas-user] Least Common Multiple
>
> If one or more of the values is zero then the LCM would have to be zero.
> That said, I think there is also a logic problem in you WHILE statement.
> With "AND" all the remainders would have to be non-zero, but as long as one
> is non-zero you need to keep looking.  I used this code to test teh LCM and
> using "OR" gave the correct result (I tested with 3, 4, 6 and 3, 5, 7).
>
> STATIC PUBLIC SUB Main()
>
>   DIM first AS Integer
>   DIM secnd AS Integer
>   DIM third AS Integer
>
>   DIM vmax AS Integer
>   DIM loops AS Integer
>
>   PRINT "Enter three numbers:"
>   INPUT first
>   INPUT secnd
>   INPUT third
>
>   vmax = Max(first, secnd, third)
>   loops = vmax
>
>   IF first <> 0 AND secnd <> 0 AND third <> 0 THEN
>
>     WHILE loops MOD first <> 0 OR loops MOD secnd <> 0 OR loops MOD third <>
> 0
>       PRINT "new = " & Str(loops)
>       loops = loops + vmax
>     WEND
>
>     PRINT "LCM = " & Str(loops)
>
>   ELSE
>
>     PRINT "LCM = 0"
>
>   END IF
>
> END
>
> Hope this helps,
> Allen
>
>
> On 4/10/06, Peter Moers <peter.moers at ...626...> wrote:
> > hi,
> >
> > I'm trying to calculate the least common multiple of 3 integers I
> > tried the following code but had to find out it doesn't work when
> > xs,ys or zs equals 0.
> >
> > DIM vmax AS Integer = Max(xs, ys, zs)
> > DIM loops AS Integer = vmax
> > WHILE loops MOD xs <> 0 AND loops MOD ys <> 0 AND loops MOD zs <> 0
> >   loops = loops + vmax
> > WEND
> >
> > Now, I can split it up in 3 loops after checking the values for not
> > being 0 but I think it must be able to do this with less code.
> > Suggestions?
> >
> > Maybe LCM could implemented into the Gambas language, would be usefull.
> >
> >
> > regards,
> >
> > --
> > Peter Moers
> > peter.moers at ...1417...
> > Divides Webdesign - http://www.divides.be
> > Startpagina - http://www.321start.be
> >
> >
> > -------------------------------------------------------
> > This SF.Net email is sponsored by xPML, a groundbreaking scripting
> language
> > that extends applications into web and mobile media. Attend the live
> webcast
> > and join the prime developer group breaking into this new coding
> territory!
> >
> http://sel.as-us.falkag.net/sel?cmdlnk&kid0944&bid$1720&dat1642
> > _______________________________________________
> > Gambas-user mailing list
> > Gambas-user at lists.sourceforge.net
> > https://lists.sourceforge.net/lists/listinfo/gambas-user
> >
>
>


--
Peter Moers
peter.moers at ...1417...
Divides Webdesign - http://www.divides.be
Startpagina - http://www.321start.be




More information about the User mailing list