2847
|
1 ## Copyright (C) 1996, 1997 John W. Eaton |
2313
|
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. |
1026
|
19 |
3321
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Mapping Function} {} lcm (@var{x}, @code{...}) |
|
22 ## Compute the least common multiple of the elements elements of @var{x}, or |
3426
|
23 ## the list of all the arguments. For example, |
|
24 ## |
3321
|
25 ## @example |
|
26 ## lcm (a1, ..., ak) |
|
27 ## @end example |
3426
|
28 ## |
3321
|
29 ## @noindent |
|
30 ## is the same as |
3426
|
31 ## |
3321
|
32 ## @example |
|
33 ## lcm ([a1, ..., ak]). |
|
34 ## @end example |
|
35 ## @end deftypefn |
3426
|
36 ## @seealso{gcd, min, max, ceil, and floor} |
2311
|
37 |
2312
|
38 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> |
|
39 ## Created: 16 September 1994 |
|
40 ## Adapted-By: jwe |
|
41 |
3979
|
42 function l = lcm (a, varargin) |
904
|
43 |
2601
|
44 if (nargin == 0) |
|
45 usage ("lcm (a, ...)"); |
|
46 endif |
|
47 |
|
48 if (nargin > 1) |
3979
|
49 k = 1; |
|
50 for i = 2:nargin |
|
51 a = [a, varargin{k++}]; |
715
|
52 endfor |
|
53 endif |
2325
|
54 |
904
|
55 if (round (a) != a) |
3458
|
56 error ("lcm: all arguments must be integer"); |
715
|
57 endif |
2325
|
58 |
904
|
59 if (any (a) == 0) |
715
|
60 l = 0; |
|
61 else |
904
|
62 a = abs (a); |
|
63 l = a (1); |
|
64 for k = 1:(length (a) - 1) |
1026
|
65 l = l * a(k+1) / gcd (l, a(k+1)); |
715
|
66 endfor |
|
67 endif |
2325
|
68 |
715
|
69 endfunction |