Finding LCM and HCF of two numbers.

 LCM: The least common multiple of a number is the smallest number that is the product of two or more numbers.

Let's check out the formula for LCM 😉:

LCM(a,b)=(a*b)/(greatest common divisor of a and b)
i.e.
LCM(a,b)=(a*b)/(HCF of a,b)

e.g LCM of 12,15 is 60

Let's check out how:

divisors of 12=1,2,3,4,6,12

divisors of 15=1,2,3,5,15

greatest common divisor=3 

LCM(12,15)=(12*15)/(3)=60


HCF: HCF of two or more numbers is the highest common factor of the given numbers.

It means the greatest number which can divide the given numbers.

Let's check out the formula for HCF 😹💭:

1. First we'll find factors of both numbers.

2. Then we'll find highest common among them. As simple as that.

e.g HCF of 12,15 is 3

Lets check how:

factors of 12=1,2,3,4,6,12

factors of 15=1,2,3,5,15

common factor=1,2,3

Highest Common Factor=3


Python Code for HCF and LCM 👇


def check_HCF(num1,num2):
    n2_divisors=[for i in range(1,num2+1) if num2%i==0]
    # we wiill reverse the num2 divisors and check if 
    highest divisor divides the num1 so that it will
    statisfy our Highest Common Factor condition.
    for i in reversed(n2_divisors):
        if num1%i==0:
            return i
            
def check_LCM(num1,num2):
    return int((num1*num2)/check_HCF(num1,num2))

if __name__=="__main__":
    a,b=map(int, input().split())
    print(check_HCF(a,b),check_LCM(a,b))


Comments

Post a Comment

Popular Posts