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

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 allFruits and vegetables drawing
~Santhosh and Sandhiya Drawing is a process that all children naturally engage in, from the time they first discover they can hold...
Read out allUsing gears for ratios
7th in Udavi children were learning ratios. We used a crank fan and used a spur gear system (in the pictures) to...
Read out allCycle Safety light
For quite a long time (year and a half), kids in the electronics lab have been working on breadboard circuits which they...
Read out all