GCD and LCM using Sympy in Python
##########Source code for finding GCD and LCM using Sympy in python#########
from sympy import *
x = Symbol('x')
expression1, expression2 = x**2 + 4*x - 12, x**2 - 5*x + 6
def findGCD(expression1, expression2):
print("########### Find GCD ############")
factorsExpr1, factorsExpr2 = factor(expression1), factor(expression2)
print("factorsExpr1 : ", factorsExpr1)
print("factorsExpr2 : ", factorsExpr2)
GCD = gcd(expression1, expression2)
print("GCD : ", GCD)
return GCD
def findLCM(expression1, expression2, GCD):
print("########### Find LCM ############")
factorsExpr1, factorsExpr2 = factor(expression1), factor(expression2)
LCM = factor(lcm(expression1, expression2))
print("LCM of Expr1 and Expr2 : ", LCM)
print("f(x)*g(x) = ", factor(expression1*expression2))
print("(f(x)*g(x))/GCD = {0}/{1}".format(factor(expression1*expression2),GCD))
LCM = simplify((factorsExpr1*factorsExpr2)/GCD)
print('Finding LCM using (f(x)*g(x))/GCD" : ', LCM)
return LCM
GCD = findGCD(expression1, expression2)
LCM = findLCM(expression1, expression2, GCD))
######## OUTPUT #############
########### Find GCD ############ factorsExpr1 : (x - 2)*(x + 6) factorsExpr2 : (x - 3)*(x - 2) GCD : x - 2 ########### Find LCM ############ LCM of Expr1 and Expr2 : (x - 3)*(x - 2)*(x + 6) f(x)*g(x) = (x - 3)*(x - 2)**2*(x + 6) (f(x)*g(x))/GCD = (x - 3)*(x - 2)**2*(x + 6)/x - 2 Finding LCM using (f(x)*g(x))/GCD" : (x - 3)*(x - 2)*(x + 6)
Author
prathap7618@gmail.com
Related Posts
A Day of Energy, Exploration, and Learning at STEM Land
-Vaishnavi Rishi Valley School Visit on 22 January On 22nd January, STEM Land welcomed students from Rishi Valley School. The group consisted...
Read out all
Do All Liquids Conduct Electricity? A Classroom Moment of Wonder
-Vaishnavi “Yes, Akka… all liquids conduct electricity!” That was the confident reply I received when I asked my students a simple question....
Read out all
5th Graders Share Their Talents at STEMland’s 10th Anniversary
-SandhiyaBala My Name is Sandhiya Bala, and I deeply care about Courage, care, and Happiness for myself and others. I was teaching...
Read out all
Exploring Awareness Through the Body (ATB) – A Teacher’s Journey
-SandhiyaBala, SivaguruPrasath and Nithya Santhosh SAIIER Teachers’ Centre recently invited teachers to a three-day workshop series called ATB for Teachers, held on...
Read out all
Plaksha School Visit
-Ilakiya, Tamil, Rajesh, Sivaguru and Aarthi STEMland was invited by Plaksha School in Andhra Pradesh for a three-day session to work with...
Read out all
Joy of Creating Puzzles in Algebraic equation
-Sribhavani New Initiative of Sanjeev for the year 2025 is to support children from 5th to 8th grade to make project and...
Read out all