Command-line Ruby quadratic solver

2 08 2010

I have been thinking a lot about school lately. I already miss it a great deal. I spent a few years studying algebra and pre-calculus topics. When I was doing homework, I found myself repeating myself in that I was doing similar problems again and again.

I fully acknowledge that there was a point in me doing all of these exercises. But there were occasions when I would *cough* *cough* run out of time and have to power through a set of problems quickly. This is what led me to the world of TIBasic programming when I was a senior in high school. I am revisiting one of my old programs and I refactored it in Ruby.

# quadratic.rb
# Written by Patrick Proctor (
# on August 2, 2010
# This is a program that accepts the three coefficients from a quadratic equation and solves for x.
# Enjoy!
#/usr/bin/env ruby
class Quadratic
def find_x(input_a, input_b, input_c)
answer = this_is_x(input_a, input_b, input_c).to_s
puts "x = #{answer}"

def this_is_x(a, b, c)
a = a.to_i
b = b.to_i
c = c.to_i

pos_x = (-b + Math.sqrt((b*b)-(4*a*c)))/(2*a)
neg_x = (-b - Math.sqrt((b*b)-(4*a*c)))/(2*a)

if pos_x != neg_x
return "#{pos_x}\nx = #{neg_x}"
return "#{pos_x}"
end[0], ARGV[1], ARGV[2])




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: