#!/usr/bin/env ruby

# Equation solver in Ruby
def solve_equation(a, b, c)
  puts "Solving: #{a}x² + #{b}x + #{c} = 0"
  
  # Linear equation (a = 0)
  if a == 0
    if b == 0
      return c == 0 ? "Infinite solutions" : "No solution"
    else
      x = -c.to_f / b
      return "Linear equation: x = #{x.round(4)}"
    end
  end
  
  # Quadratic equation
  discriminant = b*b - 4*a*c
  puts "Discriminant = #{discriminant}"
  
  if discriminant > 0
    x1 = (-b + Math.sqrt(discriminant)) / (2*a)
    x2 = (-b - Math.sqrt(discriminant)) / (2*a)
    "Two real roots: x₁ = #{x1.round(4)}, x₂ = #{x2.round(4)}"
  elsif discriminant == 0
    x = -b / (2*a)
    "One real root: x = #{x.round(4)}"
  else
    real_part = -b / (2*a).to_f
    imag_part = Math.sqrt(-discriminant) / (2*a)
    "Complex roots: x₁ = #{real_part.round(4)} + #{imag_part.round(4)}i, x₂ = #{real_part.round(4)} - #{imag_part.round(4)}i"
  end
end

# Test cases
puts "=== RUBY EQUATION SOLVER ==="
puts solve_equation(1, -3, 2)
puts solve_equation(1, 2, 1)
puts solve_equation(1, 0, 1)
puts solve_equation(0, 2, -4)
puts solve_equation(0, 0, 0)
puts solve_equation(0, 0, 5)
