octave-maintainers
[Top][All Lists]

## Re: lcm(0,n) gives 0 which should be changed.

 From: Marco Caliari Subject: Re: lcm(0,n) gives 0 which should be changed. Date: Mon, 26 Mar 2018 09:09:01 +0200

```On Sun, 25 Mar 2018 22:48:15 -0700 (MST)

> Jordi Gutiérrez Hermoso-2 wrote
> > I suppose you can argue that 0 is always the lcm of any two
> > nonnegative integers and the output of lcm should positive to rule out
> > 0. I think it makes more sense to return something here since one of
> > the inputs is zero, instead of erroring out.
>
> Forgive me, but lcm of x,y lcm(x,y) must be divisible by x and y.
> But if lcm(0,n) produces 0 that means 0 is divisible by 0.But
> 0/0 is undefined.

If we stay with the definition, clearly lcm(0,something) is not defined. But
Euclid's algorithm is used: lcm(a,b) = a*b/gcd(a,b). With this algorithm, I
think it is safe to extend the definition to lcm(0,something!=0) and get zero
as result. Then, in the current implementation there is already the
idea to set explicitely to 0 the result for lcm(0,0). This happens, but there
is a warning about division by zero. Finally, current implementation handles
also negative numbers and the result is negative if the inputs have opposite
sign. Here, I would change the definition to lcm(a,b)=|a*b|/gcd(a,b) and write
that we use this definition in the documentation.

Both wolframalpha and SageMath give:

lcm(0,3) = 3
lcm(0,-3) = 0 (the positive 0 in SageMath, not sure in wolframalpha)
lcm(0,0) = 0
lcm(1,-3) = 3
lcm(-1,-3) = 3

without any warning.

Marco

```