A function of degree n
can be written as a product of three factors:
- a constant
a
not null
- a first degree function
(x - r_n)
- a function of degree
n-1
Takingthisrecursively,weconcludethateveryfunctionofdegreen
isaproductofn
firstdegreeequations:
Tofindtherootofthefunction,weneedtomatchittozero.Sincethen
functionhasbeenrewrittenasaproduct(anda
bydefinitionisnon-zero),thismeansthatatleastoneofthefirst-degreefunctionsthatcomposethen
functionmustbezero.Andwhatisthisvalue?Itisr_i
.r_i
representstherootofoneofthefunctions.
Take,forexample,r_n
.Whenx=r_n
,wehavethefollowing:
F_n(x)=a*(x-r_n)*F_n-1(x)F_n(r_n)=a*(r_n-r_n)*F_n-1(r_n)=a*(0)*F_n-1(r_n)==>F_n(r_n)=0
RegardlessofthevalueofF_n-1(r_n)
.
Thatsaid,someconsiderations:
- Icanhave
r_i
andr_j
,fori!=j
,withr_i==r_j
;thismeansthattherootappearedmultipletimesinthefunction,butthefunctioncontinueswithn
roots - Icanhaverepeatedrootsyes
- Thetruthofthedecompositionofthatdecompositionofafunctionofdegree
n
intoafunctionofdegreen-1
appliesunderthefollowingconditions: - coefficientsof
F_n(x)
arerealcoefficients - theextractedrootisacomplexnumber(justrememberingthateveryrealnumberbelongstothesetofcomplexes)
Thatbeingsaid,thenIamasupporterthatyoushouldpresenttwoidenticalrootstotheequationandclassifythemasr_1
andr_2
.Thispreventsthe zero-comparison problem properly perceived by @AndersonCarlosWoss .
To present your roots in strictly non-decreasing order, you can take advantage of the sqrt
: return the square root of the number. By definition, the square root of a nonnegative number is a positive number. With this, I can say:
x + sqrt(y) >= x - sqrt(y)
And the equality case only happens when y == 0
.
So, I would rewrite your code this way:
import math
def delta(a,b,c):
return b**2 - 4*a*c
def main():
a = float(input("digite o valor de a: "))
b = float(input("digite o valor de b: "))
c = float(input("digite o valor de c: "))
imprime_raizes(a,b,c)
def imprime_raizes(a, b, c):
d = delta(a, b, c)
if d < 0:
print("A equação não possui raízes reais")
else:
raiz1 = (-b + math.sqrt(d))/(2 * a)
raiz2 = (-b - math.sqrt(d))/(2 * a)
print("A maior raiz é: ", raiz1)
print("A menor raiz é: ", raiz2)