Advertisement
MahdeenSky

Quadratic Factorer and Solver

Feb 22nd, 2020
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.56 KB | None | 0 0
  1. ```python```
  2. from math import sqrt, pi
  3.  
  4. print("ax^2+bx+c=0")
  5.  
  6.  
  7. def stop_or_continue():
  8.     stop_flag = False
  9.     x = ""
  10.     while x != "a" or "b":
  11.         x = input("Stop?: ")
  12.         if stop_or_continue == "a":
  13.             stop_flag = True
  14.             break
  15.         if stop_or_continue == "b":
  16.             stop_flag = False
  17.             break
  18.     if stop_flag:
  19.         exit()
  20.  
  21.  
  22. def polynomial_checker(a, b, c):
  23.     negative_factor = False
  24.     if a < 0:
  25.         negative_factor = True
  26.     #  checks if gcf can be applied to simplify the quadratic expression
  27.     b_divisible_by_a = (b % a == 0)
  28.     c_divisible_by_a = (c % a == 0)
  29.     check_divisible = (b_divisible_by_a and c_divisible_by_a)
  30.     a_is_one = (a == (a / a))
  31.     return a_is_one, check_divisible, negative_factor
  32.  
  33.  
  34. def isclose(a, b, tol):
  35.     if abs(a-b) <= tol:
  36.         return True
  37.     else:
  38.         return False
  39.  
  40.  
  41. def gcd(x, y):
  42.     while y != 0:
  43.         (x, y) = (y, x % y)
  44.     return x
  45.  
  46.  
  47. def fraction(a):
  48.     factor = 0
  49.     while True:
  50.         factor += 1
  51.         a_rounded = int(round(a*factor))
  52.         if isclose(a*factor, a_rounded, 0.01):
  53.             text = ("{}/{}".format(a_rounded, factor))
  54.             return text
  55.  
  56.  
  57. def simplify_fraction(numer, denom):
  58.     if denom == 0:
  59.         return "Division by 0 - result undefined"
  60.  
  61.     # Remove greatest common divisor:
  62.     common_divisor = gcd(numer, denom)
  63.     (reduced_num, reduced_den) = (numer / common_divisor, denom / common_divisor)
  64.     # Note that reduced_den > 0 as documented in the gcd function.
  65.  
  66.     if common_divisor == 1:
  67.         return (numer, denom)
  68.     else:
  69.         # Bunch of nonsense to make sure denominator is negative if possible
  70.         if (reduced_den > denom):
  71.             if (reduced_den * reduced_num < 0):
  72.                 return (-reduced_num, -reduced_den)
  73.             else:
  74.                 return (reduced_num, reduced_den)
  75.         else:
  76.             return (reduced_num, reduced_den)
  77.  
  78.  
  79. def complete_the_square(a, b, c, output):
  80.     a_original, b_original, c_original = float(a), float(b), float(c)
  81.     a_is_one, check_divisible, negative_factor = polynomial_checker(a_original, b_original, c_original)
  82.     last_option = ((check_divisible is False) and (a_is_one is False))
  83.     if negative_factor:
  84.         gcf = -int(gcd(int(-a_original), gcd(int(-b_original), int(-c_original))))
  85.         b_original, c_original, a_original = -b_original, -c_original, -a_original
  86.     if a_is_one or (check_divisible is False):
  87.         gcf = ""
  88.     elif check_divisible and (a_is_one is False):
  89.         if negative_factor is False:
  90.             gcf = int(gcd(int(a_original), gcd(int(b_original), int(c_original))))
  91.         b_original, c_original, a_original = b_original / a_original, c_original / a_original, a_original / a_original
  92.     elif last_option:
  93.         gcf = a
  94.         b_original, c_original, a_original = b_original / a_original, c_original / a_original, a_original / a_original
  95.  
  96.     d = b_original / (2 * a_original)
  97.     e = c_original - (b_original**2/(4 * a_original))
  98.  
  99.     if output:
  100.         print("The Factored Form is:\n{}(x{}){}".format(gcf, float(d), float(e)))
  101.     return gcf, d, e
  102.  
  103.  
  104. def quadratic_function(a, b, c, output):
  105.     complete_square = False
  106.     a_original, b_original, c_original = a, b, c
  107.     if b ** 2 - 4 * a * c >= 0:
  108.         a_is_one, check_divisible, negative_factor = polynomial_checker(a, b, c)
  109.         if negative_factor:
  110.             gcf = -int(gcd(int(-a), gcd(int(-b), int(-c))))
  111.             b, c, a = -b, -c, -a
  112.         if a_is_one or (check_divisible is False):
  113.             gcf = ""
  114.         elif check_divisible and (a_is_one is False):
  115.             if negative_factor is False:
  116.                 gcf = int(gcd(int(a), gcd(int(b), int(c))))
  117.             b, c, a = b/a, c/a, a/a
  118.         x1 = (-b + sqrt(b ** 2 - 4 * a * c)) / (2 * a)
  119.         x2 = (-b - sqrt(b ** 2 - 4 * a * c)) / (2 * a)
  120.         # Added a "-" to these next 2 values because they would be moved to the other side of the equation
  121.         mult1 = -x1 * a
  122.         mult2 = -x2 * a
  123.         (num1, den1) = simplify_fraction(a, mult1)
  124.         (num2, den2) = simplify_fraction(a, mult2)
  125.         if (num1 > a) or (num2 > a):
  126.             # acquires the complete square since it is not factorable using normal means
  127.             if output:
  128.                 complete_the_square(a_original, b_original, c_original, True)
  129.             complete_square = True
  130.             gcf, d, e = complete_the_square(a_original, b_original, c_original, False)
  131.         else:
  132.             # Getting ready to make the print look nice
  133.             if output:
  134.                 if (den1 > 0):
  135.                     sign1 = "+"
  136.                 else:
  137.                     sign1 = ""
  138.                 if (den2 > 0):
  139.                     sign2 = "+"
  140.                 else:
  141.                     sign2 = ""
  142.                 print("The Factored Form is:\n{}({}x{}{})({}x{}{})".format(gcf, int(num1), sign1, int(den1), int(num2), sign2,int(den2)))
  143.     else:
  144.         # if the part under the sqrt is negative, you have a solution with i
  145.         if output:
  146.             print("Solutions are imaginary")
  147.     return complete_square, gcf, d, e
  148.  
  149.  
  150. def solutions(a, b, c, d, e, gcf, complete_square):
  151.     if complete_square is False:
  152.         discriminant = b ** 2 - 4 * a * c
  153.         if discriminant == 0:
  154.             x_one = (-b + sqrt(b ** 2 - 4 * a * c)) / (2 * a)  # x_one
  155.             if eval(fraction(x_one)) % 1 == 0:
  156.                 print("x1 is : ", int(x_one))
  157.             else:
  158.                 print("x1 is : ", fraction(x_one))
  159.         if discriminant > 0:
  160.             x_one = (-b + sqrt(b ** 2 - 4 * a * c)) / (2 * a)  # x_one
  161.             if eval(fraction(x_one)) % 1 == 0:
  162.                 print("x1 is : ", int(x_one))
  163.             else:
  164.                 print("x1 is : ", fraction(x_one))
  165.             x_two = (-b - sqrt(b ** 2 - 4 * a * c)) / (2 * a)  # x_two
  166.             if eval(fraction(x_two)) % 1 == 0:
  167.                 print("x2 is : ", int(x_two))
  168.             else:
  169.                 print("x2 is : ", fraction(x_two))
  170.         if discriminant < 0:
  171.             pass
  172.     elif complete_square:
  173.         right_side = -e
  174.         if gcf == "":
  175.             gcf = 1
  176.         right_side = right_side/gcf
  177.         right_side_1 = sqrt(right_side)
  178.         right_side_2 = -sqrt(right_side)
  179.         x_one = right_side_1 - d
  180.         x_two = right_side_2 - d
  181.         sign_one = "+"
  182.         sign_two = "-"
  183.         print("x1 is : {}{}sqrt({})/{} or {} ".format(-d, sign_one, right_side, gcf, round(x_one,3)))
  184.         print("x2 is : {}{}sqrt({})/{} or {} ".format(-d, sign_two, right_side, gcf, round(x_two,3)))
  185.  
  186.  
  187. while True:
  188.     try:
  189.  
  190.         a = float(eval(input("insert a: ").replace("pi", str(pi))))
  191.         b = float(eval(input("insert b: ").replace("pi", str(pi))))
  192.         c = float(eval(input("insert c: ").replace("pi", str(pi))))
  193.  
  194.         quadratic_function(a, b, c, True)
  195.         complete_square, gcf, d, e = quadratic_function(a, b, c, False)
  196.  
  197.         solutions(a, b, c, d, e, gcf, complete_square)
  198.  
  199.         stop_or_continue()
  200.  
  201.     except ValueError:
  202.         pass
  203. ```python```
  204.  
  205. input: a = 5
  206.        b = 20
  207.        c = 32
  208.  
  209. output:
  210.     Traceback (most recent call last):
  211.   File "E:\Utilities and Apps\Python\MY PROJECTS\Quadratic Polynomial Factorer and Solver v2.py", line 193, in <module>
  212.     quadratic_function(a, b, c, True)
  213.   File "E:\Utilities and Apps\Python\MY PROJECTS\Quadratic Polynomial Factorer and Solver v2.py", line 146, in quadratic_function
  214.     return complete_square, gcf, d, e
  215. UnboundLocalError: local variable 'gcf' referenced before assignment
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement