NumPy LCM Lowest Common Multiple

🔢 NumPy LCM (Lowest Common Multiple)

NumPy provides a fast and vectorized function np.lcm() to compute the lowest common multiple of integers element-wise.


1. Basic LCM of Two Numbers


 

  • LCM of 12 and 15 is 60 → smallest number divisible by both


 2. LCM of Arrays


 

  • Computes element-wise LCM

  • lcm_arr[i] = LCM(arr1[i], arr2[i])


 3. LCM with Scalars and Arrays


 

  • Scalar is broadcasted to array automatically


 4. Using LCM for Multiple Numbers

  • NumPy lcm works element-wise. For multiple numbers, you can use reduce:


  • Computes LCM of all elements in the array


 5. Notes & Tips

  • LCM is always ≥ max(a, b)

  • Useful in fractions, synchronization, scheduling, or cyclic events

  • Works with integers only


🎯 Practice Exercises

  1. Find the LCM of 18 and 24.

  2. Find element-wise LCM of arrays [3,4,5] and [6,8,10].

  3. Find the LCM of [4, 6, 8, 12] using np.lcm.reduce().

You may also like...