Quadratic formula in PythonΒΆ

Quadratic formula:

import math

a = 3.0
b = -2.0
c = -3.0
det = b * b - 4.0 * a * c
if det < 0:
    num_roots = 0
r1 = (-b + math.sqrt(det)) / (2.0 * a)
if det == 0:
    num_roots = 1
else:
    num_roots = 2
r2 = (-b - math.sqrt(det)) / (2.0 * a)
print '%d roots' % num_roots
if num_roots == 1:
    print 'Root: %.2f' % r1
elif num_roots == 2:
    print "Root 1: %.2f - Root 2: %.2f\n" % (r1, r2)

Quadratic formula in a function:

import math

def quad(a, b, c):
    det = b * b - 4.0 * a * c
    if det < 0:
        return []
    r1 = (-b + math.sqrt(det)) / (2.0 * a)
    if det == 0:
        return [r1]
    else:
        r2 = (-b - math.sqrt(det)) / (2.0 * a)
        return [r1, r2]

a = 3.0
b = -2.0
c = -3.0

res = quad(a, b, c)

print '%d roots' % len(res)
for r in res:
    print 'Root: %.2f' % r

Several quadratic equations:

import math

def quad(a, b, c):
    det = b * b - 4.0 * a * c
    if det < 0:
        return []
    r1 = (-b + math.sqrt(det)) / (2.0 * a)
    if det == 0:
        return [r1]
    else:
        r2 = (-b - math.sqrt(det)) / (2.0 * a)
        return [r1, r2]

def run(a, b, c):
    res = quad(a, b, c)
    print '%d roots' % len(res)
    for r in res:
        print 'Root: %.2f' % r

run(1.0, -2.0, -3.0)
run(-1.0, -1.0, -1.0)
run(2.0, 2.0, 0.5)

Equation definition from a file:

import math

def quad(a, b, c):
    det = b * b - 4.0 * a * c
    if det < 0:
        return []
    r1 = (-b + math.sqrt(det)) / (2.0 * a)
    if det == 0:
        return [r1]
    else:
        r2 = (-b - math.sqrt(det)) / (2.0 * a)
        return [r1, r2]

def run(a, b, c):
    res = quad(a, b, c)
    print '%d roots' % len(res)
    for r in res:
        print 'Root: %.2f' % r

with open('test1.txt', 'r') as f:
    text = f.read()
    words = text.split()
    numbers = list()
    for word in words:
        numbers.append(float(word))
    if len(numbers) != 3:
        print >> sys.stderr, 'Could not parse'
    else:
        run(numbers[0], numbers[1], numbers[2])

Several quadratic equations from a file:

import math
import sys

def quad(a, b, c):
    det = b * b - 4.0 * a * c
    if det < 0:
        return []
    r1 = (-b + math.sqrt(det)) / (2.0 * a)
    if det == 0:
        return [r1]
    else:
        r2 = (-b - math.sqrt(det)) / (2.0 * a)
        return [r1, r2]

max_root = -1
max_a = -1
max_b = -1
max_c = -1
with open('test.txt', 'r') as f:
    for line in f:
        words = line.split()
        numbers = list()
        for word in words:
            numbers.append(float(word))
        if len(numbers) != 3:
            print >> sys.stderr, 'Could not parse'
        else:
            roots = quad(numbers[0], numbers[1], numbers[2])
            for root in roots:
                if root > max_root:
                    max_root = root
                    max_a = numbers[0]
                    max_b = numbers[1]
                    max_c = numbers[2]

    print 'Maximum root is %.2f for equation (%.3f, %.3f, %.3f)' % (max_root, max_a, max_b, max_c)