function c = lcm(a,b)
%MATLAB Code Generation Library Function

%   Copyright 2006-2014 The MathWorks, Inc.
%#codegen

eml_invariant(nargin == 2,'MATLAB:minrhs');
eml_invariant(coder.internal.isBuiltInNumeric(a), ...
    'Coder:toolbox:unsupportedClass','lcm',class(a));
eml_invariant(coder.internal.isBuiltInNumeric(b), ...
    'Coder:toolbox:unsupportedClass','lcm',class(b));
eml_invariant(isreal(a) && isreal(b),'Coder:toolbox:lcm_4');
if notPosInts(a) || notPosInts(b)
    coder.internal.error('MATLAB:lcm:InputNotPosInt');
end
c = coder.internal.applyBinaryScalarFunction(mfilename, ...
    coder.internal.scalarEg(a,b),@scalar_lcm,a,b);

%--------------------------------------------------------------------------

function c = scalar_lcm(a,b)
c = gcd(a,b);
if isinteger(a)
    c = eml_rdivide(a,c,class(c),'spill').*b;
elseif isinteger(b)
    c = a.*eml_rdivide(b,c,class(c),'spill');
else
    c = a.*(b./c);
end

%--------------------------------------------------------------------------

function p = notPosInts(a)
p = false;
if ~coder.internal.isaUint(a)
    for k = 1:numel(a)
        if floor(a(k)) ~= a(k) || a(k) < 1
            p = true;
            break
        end
    end
end

%--------------------------------------------------------------------------
