Skip to content

Commit a318d63

Browse files
authored
Create quadratic-solver.py
1 parent 0e1d742 commit a318d63

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

quadratic-solver.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Written by Ivan Gonzalez (2017)
2+
from math import sqrt, fmod
3+
4+
def mcd(a, b, *args):
5+
a, b = abs(a), abs(b)
6+
if a<b:
7+
a, b = b, a
8+
while b:
9+
a, b = b, fmod(a,b)
10+
if args:
11+
return mcd(a, args[0], *args[1:])
12+
else:
13+
return a
14+
15+
def solve(a, b=0, c=0):
16+
q = mcd(a, b, c)
17+
try:
18+
x = [(-b+sqrt(b**2-4*(a*c)))/(2*a), (-b-sqrt(b**2-4*(a*c)))/(2*a)]
19+
except Exception:
20+
print 'No solutions'
21+
else:
22+
print 'For {}(x^2) {:+}(x) {:+}'.format(a, b, c), 'simplified -> {}*[{}(x^2) {:+}(x) {:+}]'.format(q, a/q, b/q, c/q) if q is not 1 else ''
23+
print 'Solution {}(x{:+})*(x{:+})'.format(str(q)+'*' if q is not 1 else '', -x[0], -x[1])
24+
25+
a = input('a=')
26+
b = input('b=')
27+
c = input('c=')
28+
solve(a,b,c)

0 commit comments

Comments
 (0)