home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21fb.zip / octave / SCRIPTS.ZIP / scripts / elfun / lcm.m < prev    next >
Encoding:
Text File  |  1999-10-26  |  1.7 KB  |  71 lines

  1. ## Copyright (C) 1996, 1997 John W. Eaton
  2. ##
  3. ## This file is part of Octave.
  4. ##
  5. ## Octave is free software; you can redistribute it and/or modify it
  6. ## under the terms of the GNU General Public License as published by
  7. ## the Free Software Foundation; either version 2, or (at your option)
  8. ## any later version.
  9. ##
  10. ## Octave is distributed in the hope that it will be useful, but
  11. ## WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. ## General Public License for more details.
  14. ##
  15. ## You should have received a copy of the GNU General Public License
  16. ## along with Octave; see the file COPYING.  If not, write to the Free
  17. ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  18. ## 02111-1307, USA.
  19.  
  20. ## -*- texinfo -*-
  21. ## @deftypefn {Mapping Function} {} lcm (@var{x}, @code{...})
  22. ## Compute the least common multiple of the elements elements of @var{x}, or
  23. ## the list of all the arguments.  For example, 
  24. ## 
  25. ## @example
  26. ## lcm (a1, ..., ak)
  27. ## @end example
  28. ## 
  29. ## @noindent
  30. ## is the same as
  31. ## 
  32. ## @example
  33. ## lcm ([a1, ..., ak]).
  34. ## @end example
  35. ## @end deftypefn
  36.  
  37. ## See also: gcd, min, max, ceil, floor.  
  38.  
  39. ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at>
  40. ## Created: 16 September 1994
  41. ## Adapted-By: jwe
  42.  
  43. function l = lcm (a, ...)
  44.  
  45.   if (nargin == 0)
  46.     usage ("lcm (a, ...)");
  47.   endif
  48.  
  49.   if (nargin > 1)
  50.     va_start;
  51.     for k = 2:nargin;
  52.       a = [a, (va_arg ())];
  53.     endfor
  54.   endif
  55.  
  56.   if (round (a) != a)
  57.     error ("lcm:  all arguments must be integer");
  58.   endif
  59.  
  60.   if (any (a) == 0)
  61.     l = 0;
  62.   else
  63.     a = abs (a);
  64.     l = a (1);
  65.     for k = 1:(length (a) - 1)
  66.       l = l * a(k+1) / gcd (l, a(k+1));
  67.     endfor
  68.   endif
  69.  
  70. endfunction
  71.