1## Copyright (C) 1994, 1995, 1996, 1997, 1999, 2000, 2002, 2004, 2005,
2## 2006, 2007, 2008, 2009 John W. Eaton
4## This file is part of Octave.
6## Octave is free software; you can redistribute it and/or modify it
7## under the terms of the GNU General Public License as published by
8## the Free Software Foundation; either version 3 of the License, or (at
9## your option) any later version.
11## Octave is distributed in the hope that it will be useful, but
12## WITHOUT ANY WARRANTY; without even the implied warranty of
13## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14## General Public License for more details.
16## You should have received a copy of the GNU General Public License
17## along with Octave; see the file COPYING. If not, see
18## <http://www.gnu.org/licenses/>.
21## @deftypefn {Mapping Function} {} lcm (@var{x})
22## @deftypefnx {Mapping Function} {} lcm (@var{x}, @dots{})
23## Compute the least common multiple of the elements of @var{x}, or
24## of the list of all arguments. For example,
27## lcm (a1, @dots{}, ak)
34## lcm ([a1, @dots{}, ak]).
37## All elements must be the same size or scalar.
38## @seealso{factor, gcd}
41## Author: KH <Kurt.Hornik@wu-wien.ac.at>
42## Created: 16 September 1994
45function l = lcm (varargin)
55 error ("lcm: all arguments must be integer");
63 for k = 1:(length (a) - 1)
64 l = l * a(k+1) / gcd (l, a(k+1));
80 elseif (numel (a) != 1)
81 error ("lcm: all arguments must be the same size or scalar");
86 error ("lcm: all arguments must be integer");
89 idx = find (l == 0 || a == 0);
91 l = l .* a ./ gcd (l, a);
98%!assert(lcm (3, 5, 7, 15) == lcm ([3, 5, 7, 15]) && lcm ([3, 5, 7,15]) == 105);