TDT4109/Exercise 2/11 - Andregradsligning/task11b.py

23 lines
589 B
Python
Raw Normal View History

2020-09-14 12:29:51 +02:00
from math import sqrt
2020-09-16 15:01:12 +02:00
from task11a import getValues
2020-09-14 12:29:51 +02:00
2020-09-16 15:01:12 +02:00
if __name__ == "__main__":
a, b, c = getValues()
d = b**2 - 4 * a * c
2020-09-14 12:29:51 +02:00
2020-09-16 15:01:12 +02:00
expression = f'{a}x^2 + {b}x + {c}'
if d > 0:
roots = (
(-b + sqrt(d)) / (2 * a),
(-b - sqrt(d)) / (2 * a)
)
print(
f'Andregradsligningen {expression} har de to reelle løsningene {roots[0]} og {roots[1]}'
)
elif d == 0:
root = (-b + sqrt(d)) / (2 * a)
print(f'Andregradsligningen {expression} har en reell dobbelrot {root}')
else:
print(f'Andregradsligningen {expression} har to imaginære løsninger')