0

**I am trying to make a function return the results of a Quadratic equation, But I can't figure out how to print the solutions as fraction. Please help me! **

def cube_root(x):
  return x**(1/3)
def  Quadratic(a, b, c):
  delta = (b**2)-4*a*c
  if delta == 0:
    x = (-b)/2*a
    return f"This Quadratic equation has 1 solution: {x}"
  else:
    if delta  < 0 :
      return "This Quadratic equation has no solutions: "
    else:
      x1 = ((-b)-cube_root(delta))/2*a
      x2 = ((-b)+cube_root(delta))/2*a 
      return f"This Quadratic equation has 2 solutions: {x1} & {x2}"


print(Quadratic(12, 0, -1))
  • 2
    What is "that" that it shouldn't be like? – Sayse Aug 04 '20 at 11:29
  • When i print the result, it looks like that : This Quadratic equation has 2 solutions: -41.569219381653056 & 41.569219381653056 – BeginnerProgrammer Aug 04 '20 at 11:31
  • 1
    By the way, you know that the quadratic formula has a square root where you have used a cube root? – alani Aug 04 '20 at 11:32
  • Does this help? https://stackoverflow.com/questions/23344185/how-to-convert-a-decimal-number-into-fraction – krxat Aug 04 '20 at 11:33
  • These should be square roots, not cubic roots. And you would need more than fractions to print the results, as they will very, very often be irrational. – Thierry Lathuille Aug 04 '20 at 11:33
  • Exactly. Need a package that can manipulate surds. `simplify` perhaps? – alani Aug 04 '20 at 11:34
  • 1
    @BeginnerProgrammer Please don't correct your code in reaction to comments/answers, this makes the comments (and potential answers) irrelevant. It's better to only update your question to add additionnal information/clarification. – Thierry Lathuille Aug 04 '20 at 11:36
  • Please read [mcve] and enhance your question accordingly. TELL us what you expect/want, and what actually comes out of using your code please. – GhostCat Aug 04 '20 at 11:40

2 Answers2

0

You can use simplify from the sympy package (not in the standard library - you'll have to install it):

from sympy import simplify, sqrt

def quadratic(a, b, c):
    a = simplify(a)  # convert inputs into objects used by simplify
    b = simplify(b)
    c = simplify(c)

    delta = (b**2)-4*a*c
    if delta == 0:
        x = (-b)/2*a
        return f"This Quadratic equation has 1 solution: {x}"
    elif delta < 0 :
        return "This Quadratic equation has no real solutions: "
    else:
        x1 = ((-b)-sqrt(delta))/2*a  # using sqrt from sympy
        x2 = ((-b)+sqrt(delta))/2*a
        return f"This Quadratic equation has 2 solutions: {x1} & {x2}"

print(quadratic(12, 0, -1))

This gives:

This Quadratic equation has 2 solutions: -24*sqrt(3) & 24*sqrt(3)

Different example:

print(quadratic(12, 2, -1))

gives:

This Quadratic equation has 2 solutions: -12*sqrt(13) - 12 & -12 + 12*sqrt(13)

Actually sympy can also handle complex numbers for you, so you can get rid of your test for no real solutions (i.e. remove the elif, so that delta < 0 is handled by the else: block).

If you do this and then give it the example:

print(quadratic(12, 2, 1))

you get:

This Quadratic equation has 2 solutions: -12 - 12*sqrt(11)*I & -12 + 12*sqrt(11)*I
alani
  • 12,573
  • 2
  • 13
  • 23
0

If you don't want extra packages, perhaps the following might help:

from fractions import Fraction

def is_square(x):
    if x < 0: return False
    s = int(x**0.5)
    return s*s == x

def sqrt_frac_str(frac):
    if frac < 0:
        return f'i {sqrt_frac_str(-frac)}'
    num_isq = is_square(frac.numerator)
    den_isq = is_square(frac.denominator)
    if num_isq and den_isq:
        return f'{int(frac.numerator**0.5)}/{int(frac.denominator**0.5)}'
    elif num_isq:
        return f'{int(frac.numerator**0.5)}/sqrt({frac.denominator})'
    elif den_isq:
        return f'sqrt({frac.numerator})/{int(frac.denominator**0.5)}'
    else:
        return f'sqrt({frac})'

def  quadratic_frac(a, b, c):
    delta = Fraction(b**2 - 4 * a * c)
    rootcenter = Fraction(-b, 2 * a)
    rootdeltasq = delta / Fraction(2 * a)**2
    return rootcenter, rootdeltasq

def quadsol_str(rootcenter, rootdeltasq):
    return f'{rootcenter} +/- {sqrt_frac_str(rootdeltasq)}'

Tests:

rc, rd = quadratic_frac(2, 1, -1)
rc, rd
# (Fraction(-1, 4), Fraction(9, 16))

quadsol_str(*quadratic_frac(2, 1, -1))
# '-1/4 +/- 3/4'

quadsol_str(*quadratic_frac(2, 0, -1))
# '0 +/- 1/sqrt(2)'

quadsol_str(*quadratic_frac(2, 0, 1))
# '0 +/- i 1/sqrt(2)'

quadsol_str(*quadratic_frac(3, 2, -1))
# '-1/3 +/- 2/3'

quadsol_str(*quadratic_frac(5, 3, -7))
# '-3/10 +/- sqrt(149)/10'
Pierre D
  • 24,012
  • 7
  • 60
  • 96