Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """QuadriaticEquation.py by gauravssnl
- script takes quadriatic equation as input and find its root/solutions .Input Example: 1*x^2+4*x^1+4=0 """
- _author__ = "gauravssnl"
- import re
- from math import sqrt
- eqn = raw_input("Equation: ")
- pat = re.compile( "([+-]?\d+)\*?x\^2([+-]\d+)\*?x\^1([+-]\d+)=0" )
- res = pat.match(eqn)
- if res :
- # print( res.groups() )
- a,b,c= [eval(x) for x in res.groups() ]
- # Discriminant value
- D = b*b-4*a*c
- # real roots/solutions
- if D>= 0 :
- x1 = ( -b+sqrt(D) ) / (2*a)
- x2 = ( -b-sqrt(D) ) / (2*a)
- #imaginary roots/solutions
- else :
- i = sqrt(-D)
- x1 = (-b + eval("%sj"%i)) / (2*a)
- x2 = (-b - eval("%sj"%i)) / (2*a)
- print("solutions:")
- print(x1)
- print(x2)
- else:
- print("Invalid Input")
- print("Input Example: 1*x^2+4*x^1+4=0")
Add Comment
Please, Sign In to add comment