Factoring Polynomials

C

Collin D

You need to put your denominator, 2*a, in parens. The way it stands,
you are dividing by 2, then multiplying by a. That's not what you
want.

Also, for better style, I suggest you compute the discriminanat once
and store it for reuse rather than repeating the expression three
times.- Hide quoted text -

- Show quoted text -

I see what you mean on the denominator and discriminant. Ill do that.
 
C

Collin D

I see what you mean on the denominator and discriminant. Ill do that.- Hide quoted text -

- Show quoted text -

UPDATE:

#import
from math import sqrt

# collect data
a = float(raw_input('Type a value: '))
b = float(raw_input('Type b value: '))
c = float(raw_input('Type c value: '))

# find discriminant
disc = b**2 - 4*a*c

# create solver
def solver(a,b,c):
if disc < 0:
return 'No real solution.'
else:
sol1 = (-1 * b + (sqrt(disc))) / (2*a)
sol2 = (-1 * b - (sqrt(disc))) / (2*a)
return (sol1, sol2)

# execute
print solver(a,b,c)
 
R

Russ P.

UPDATE:

#import
from math import sqrt

# collect data
a = float(raw_input('Type a value: '))
b = float(raw_input('Type b value: '))
c = float(raw_input('Type c value: '))

# find discriminant
disc = b**2 - 4*a*c

# create solver
def solver(a,b,c):
    if disc < 0:
        return 'No real solution.'
    else:
        sol1 = (-1 * b + (sqrt(disc))) / (2*a)
        sol2 = (-1 * b - (sqrt(disc))) / (2*a)
        return (sol1, sol2)

# execute
print solver(a,b,c)

A couple of style points. I would use -b rather than -1 * b. Also, you
don't need the else clause. You can simplify it to

def solver(a, b, c):

disc = b**2 - 4 * a * c

if disc < 0: return "No real solution."

sol1 = (-b + sqrt(disc)) / (2*a)
sol2 = (-b - sqrt(disc)) / (2*a)

return sol1, sol2
 
R

Russ P.

A couple of style points. I would use -b rather than -1 * b. Also, you
don't need the else clause. You can simplify it to

def solver(a, b, c):

    disc = b**2 - 4 * a * c

    if disc < 0: return "No real solution."

    sol1 = (-b + sqrt(disc)) / (2*a)
    sol2 = (-b - sqrt(disc)) / (2*a)

    return sol1, sol2

By the way, this little function is suitable for your purposes here,
but it is not really a good general quadratic solver, because it
returns different types depending on the inputs. For a good general
function, you would probably want to throw an exception when the
discriminant is negative (as an earlier poster suggested).
 
C

Collin D

A couple of style points. I would use -b rather than -1 * b. Also, you
don't need the else clause. You can simplify it to

def solver(a, b, c):

    disc = b**2 - 4 * a * c

    if disc < 0: return "No real solution."

    sol1 = (-b + sqrt(disc)) / (2*a)
    sol2 = (-b - sqrt(disc)) / (2*a)

    return sol1, sol2- Hide quoted text -

- Show quoted text -

UPDATE:

#import
from math import sqrt

# collect data
a = float(raw_input('Type a value: '))
b = float(raw_input('Type b value: '))
c = float(raw_input('Type c value: '))

# create solver
def solver(a,b,c):
disc = b**2 - 4*a*c
if disc < 0:
return 'No real solution.'
else:
sol1 = (-b + (sqrt(disc))) / (2*a)
sol2 = (-b - (sqrt(disc))) / (2*a)
return (sol1, sol2)

# execute
print solver(a,b,c)
 
J

James Mills

UPDATE:

#import
from math import sqrt

# collect data
a = float(raw_input('Type a value: '))
b = float(raw_input('Type b value: '))
c = float(raw_input('Type c value: '))

# create solver
def solver(a,b,c):
disc = b**2 - 4*a*c
if disc < 0:
return 'No real solution.'
else:
sol1 = (-b + (sqrt(disc))) / (2*a)
sol2 = (-b - (sqrt(disc))) / (2*a)
return (sol1, sol2)

You do not need the else here.
return will always return control
to the calee.

Try this:

class Unsolveable(Exception): pass

# create solver
def solver(a, b, c):
disc = b**2 - 4*a*c
if disc < 0:
raise Unsolveable()

return -b + (sqrt(disc) / (2*a)), -b - (sqrt(disc) / (2*a))
 
T

Terry Reedy

Collin said:
UPDATE:
'

#import
from math import sqrt

# collect data
a = float(raw_input('Type a value: '))
b = float(raw_input('Type b value: '))
c = float(raw_input('Type c value: '))

# create solver
def solver(a,b,c):
if b**2 - 4*a*c < 0:
return 'No real solution.'
else:
sol1 = (-1 * b + (sqrt(b**2 - 4*a*c))) / 2*a
sol2 = (-1 * b - (sqrt(b**2 - 4*a*c))) / 2*a
return (sol1, sol2)

# execute
print solver(a,b,c)

TEST your code.
 
T

Tim Rowe

2008/12/18 Scott David Daniels said:
def quadsolve(a, b, c):
try:
discriminant = sqrt(b**2 - 4 * a * c)

The discriminant of a quadratic is more usually just the b**2 - 4 * a
* c part, not the square root of it. Testing that for negative, zero
or positive avoids the need to use an exception for a normal case.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top