02 Apr Numpy LCM and HCF
Let us see how to find the LCM and HCF of two numbers in Numpy. The numpy.lcm() is used to find the LCM, whereas numpy.gcd() method returns the GCD in Python.
LCM of two numbers in Numpy
LCM is the least common multiple of two integers. To find the LCM (Least Common Multiple) of two numbers, NumPy has a method lcm().
Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 |
import numpy as np # Two numbers a = 3 b = 12 # Finding LCM res = np.lcm(a, b) print("LCM of two numbers = ", res) |
Output
1 2 3 |
LCM of two numbers = 12 |
HCF/ GCD of two numbers in Numpy
The GCD of two numbers is the greatest number that divides both the integers. To find the HCF/ GCD of two numbers, Numpy has a method gcd().
Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 |
import numpy as np # Two numbers a = 2 b = 8 # finding GCD (HCF) res = np.gcd(a, b) print("GCD of two numbers = ", res) |
Output
1 2 3 |
GCD of two numbers = 2 |
If you liked the tutorial, spread the word and share the link and our website Studyopedia with others:
For Videos, Join Our YouTube Channel: Join Now
Read More:
No Comments