Skip to content

Instantly share code, notes, and snippets.

@Arafat245
Last active January 4, 2017 12:08
Show Gist options
  • Save Arafat245/053565aa61cdfe0ff53b88e4f1277266 to your computer and use it in GitHub Desktop.
Save Arafat245/053565aa61cdfe0ff53b88e4f1277266 to your computer and use it in GitHub Desktop.
import math
from functools import reduce
def primeFactor(num):
pFactorList = []
factor = 2
while factor <= math.sqrt(num):
if num % factor == 0:
counter = 0
while num % factor == 0:
num = num / factor
counter = counter + 1
for i in range(0,counter):
pFactorList.append(factor)
factor = factor + 1
if num > 1:
pFactorList.append(int(num))
return pFactorList
def gcdFinder(num1, num2):
factorization = primeFactor(num1)
factorization2 = primeFactor(num2)
print(factorization)
print(factorization2)
gcdList = []
i = 0
j = 0
k = 0
while i < len(factorization) and j < len(factorization2):
if factorization[i] == factorization2[j]:
gcdList.append(factorization[i])
del factorization[i]
del factorization2[j]
else:
if len(factorization) > len(factorization2):
if i == len(factorization)-1:
j = j + 1
i = 0
else:
i = i + 1
else:
if j == len(factorization2)-1:
i = i + 1
j = 0
else:
j = j + 1
if not gcdList or not factorization or not factorization2:
gcdList.append(1)
factorization.append(1)
factorization2.append(1)
print("\nGCD List = " + str(gcdList))
print("\nRemaining List1 = " + str(factorization))
print("Remaining List2 = " + str(factorization2))
gcd = reduce(lambda x, y: x*y, gcdList)
rem1 = reduce(lambda x,y: x*y, factorization)
rem2 = reduce(lambda x,y: x*y, factorization2)
return (gcd,rem1,rem2)
number = float(input("Please give a number: "))
number2 = float(input("Please give a number: "))
(gcd, rem1, rem2) = gcdFinder(number, number2)
print("GCD = " + str(gcd))
lcm = gcd * rem1 * rem2
print("LCM = " + str(lcm))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment