fork download
  1. def sqrt(n):
  2. x = n
  3. y = 1.0
  4. eps = 0.00001
  5. while x - y > eps:
  6. x = (x + y) / 2
  7. y = n / x
  8. return x
  9. # Viete's rules = Relatiile lui Viete
  10. # ax^2 + bx + c = 0; a != 0
  11. # x^2 - Sx + P = 0
  12. # S = x1 + x2
  13. # P = x1 * x2
  14. # S = -b/a; P = c/a
  15. # the study of the sign roots of a Quadratic Equation
  16. def Nature_Roots_Quadratic_Equation(a, b, c):
  17. d = b ** 2 - 4 * a * c
  18. #a!=0
  19. S = -b/a
  20. P = c/a
  21.  
  22. print(f"Sum = {S}, Prod = {P}")
  23. print("Sum = %.2f Prod = %.2f"%(S, P))
  24. if d < 0:
  25. return ["imaginary"]
  26. elif d >= 0:
  27. x1 = ( -b - sqrt(d) )/ (2*a)
  28. x2 = ( -b + sqrt(d) ) / (2*a)
  29. print(f"x1={x1:.2f} x2={x2:.2f}")
  30. if S > 0:
  31. if P > 0:
  32. return ["x1>0","x2>0"]
  33. elif P < 0:
  34. return ["x1<0","x2>0","|x1|<|x2|"]
  35. #x1=-3, x2=7
  36. else:
  37. return ["x1=0","x2>0"]
  38. # S < 0
  39. else:
  40. if P > 0:
  41. return ["x1<0","x2<0"]
  42. elif P < 0:
  43. return ["x1<0","x2>0","|x1|>|x2|"]
  44. else:
  45. return ["x1=0","x2<0"]
  46. def main():
  47.  
  48. lesson = dict(title = "Viete's Rules", content ="\nNature Roots Quadratic Equation")
  49.  
  50. print("Today we'll learn about:\n{title} {content}".format(**lesson))
  51.  
  52. A = 1
  53. B = 5
  54. C = 2
  55. print(*Nature_Roots_Quadratic_Equation(A,B,C), sep="\n")
  56.  
  57. main()
  58.  
Success #stdin #stdout 0.04s 9788KB
stdin
Standard input is empty
stdout
Today we'll learn about:
Viete's Rules 
Nature Roots Quadratic Equation
Sum = -5.0, Prod = 2.0
Sum = -5.00 Prod = 2.00
x1=-4.56 x2=-0.44
x1<0
x2<0