Mercurial > octave-nkf
diff scripts/elfun/lcm.m @ 715:6544b83ef9e9
[project @ 1994-09-20 18:40:02 by jwe]
Initial revision
author | jwe |
---|---|
date | Tue, 20 Sep 1994 18:40:02 +0000 |
parents | |
children | 3470f1e25a79 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/scripts/elfun/lcm.m Tue Sep 20 18:40:02 1994 +0000 @@ -0,0 +1,32 @@ +function l = lcm (a, ...) + + # lcm (a) returns the least common multiple of the entries of the + # integer vector a. + # lcm (a1, ..., ak) is the same as lcm([a1, ..., ak]). + + # Written by KH (Kurt.Hornik@ci.tuwien.ac.at) on Sep 16, 1994 + # Copyright Dept of Statistics and Probability Theory TU Wien + + if (nargin > 1) + va_start; + for k=2:nargin; + a = [a, va_arg()]; + endfor + endif + + if (round(a) != a) + error("lcm: all arguments must be integer"); + endif + + if (any(a) == 0) + l = 0; + else + a = abs(a); + l = a(1); + for k=1:(length(a)-1) + l = l * a(k+1) / gcd(l, a(k+1)); + endfor + endif + +endfunction + \ No newline at end of file