Least Common Factor - Python


Least Common Factor - Python


Hi Friends
Already we seen HCF
Similar method and function here used to calculate LCM
This method so simple... If you not seen my HCF code kindly visit as well.




CODE:
-----

# find LCM for two numbers
from collections import Counter
a = int(input('Enter first number: '))
b = int(input('Enter second number: '))
sum,a_f,b_f,sum1 = a,[],[],b

while True:
    for i in range(2,a+1):
        if sum % i == 0:
            sum = sum // i
            a_f.append(i)
            break
    if sum == 1:
        break

while True:
    for i in range(2,b+1):
        if sum1 % i == 0:
            sum1 = sum1 // i
            b_f.append(i)
            break
    if sum1 == 1:
        break


print(a_f,b_f)
c1 = Counter(a_f)
c2 = Counter(b_f)
d = list((c1 & c2).elements())
d2 = list((c1 - c2).elements())
d3 = list((c2 - c1).elements())
d.extend(d2)
d.extend(d3)
total =1
for i in d:
    total *= i
print(f'Least common factor: {total}')

-----


By
PyKAR

Comments

Popular posts from this blog

Hardy - Ramanujan Number (Magic Number) Program in Python

FLAMES CODE ON PYTHON

Count Swap for Sorting in Python