Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

89
Vistas
Trying to find LCM using recursion...I get infinity

EDIT** Reading my question, I realize now why I get infinity, num2 and num1 will never be equal since num2 will always be a larger number. Is it possible to solve this using recursion or is it better to use something like a loop? I'm able to solve this, just curious about recursion.

If both numbers are the same, I should have found the LCM and want to return it. If they are not equal, I'll call my function again, but with each increased by its original value.

 let leastCommonMultiple = (num1, num2) => {
    if (num1 === num2) return num1;

    return leastCommonMultiple(num1 + num1, num2 + num2)
}

console.log(leastCommonMultiple(4, 6)); // 12
console.log(leastCommonMultiple(3, 5)); // 15
console.log(leastCommonMultiple(2, 10)); // 10
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You certainly can write a recursive version of this. Here's one particular inelegant and inefficient version:

const lcm = (a, b, x = a, y = b) =>
  x == y 
    ? x 
    : x + a > y + b 
      ? lcm (a, b, x, y + b) 
      : lcm (a, b, x + a, y)

console .log (lcm (660, 126)) //=> 13860

We use x and y as continuing multiples of a and b respectively, increasing the smaller one until it's as least as large as the other. When they're equal, you've hit the LCM.

With a little thought, I'm sure we could make this more efficient by multiplying the right factor. But I'm not sure there's any good reason for this when GCD is so straightforward to do efficiently.

Update

Here is a more efficient version:

const lcm = (a, b, x = a, y = b) =>
  x == y 
    ? x 
    : x > y 
      ? lcm (a, b, x, b * Math.ceil (x / b)) 
      : lcm (a, b, a * Math.ceil (y / a), y)

console .log (lcm (660, 126)) //=> 13860

This uses something vaguely analogous to the modulo operator you would use for GCD, finding the first multiple of one value at least as large as another. This should be relatively efficient.

Update 2

To show how the ternaries work, this is an equivalent more imperative version:

const lcm = (a, b, x = a, y = b) => {
  if (x == y) {
    return x
  } else {
    if (x > y) {
      return lcm (a, b, x, b * Math.ceil (x / b)) 
    } else {
      return lcm (a, b, a * Math.ceil (y / a), y)
    }
  }
}

console .log (lcm (660, 126)) //=> 13860

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda