[Gambas-user] Least Common Multiple

Allen Murphy msumurph at ...626...
Tue Apr 11 03:19:36 CEST 2006


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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20060410/b8825a4d/attachment.html>


More information about the User mailing list