How do I make this app factor a polynomial with quadratic formula or some other trick?

85 Views Asked by At

How do I make this app factor a polynomial with quadratic formula or some other trick?.

The polynomial 3x^2 +10x -8 can be factored to (x +4)(3x -2) with the use of grouping or the X trick. I would like my code to do this but I have no clue how to accomplish it. My code factors the polynomial to (x -0.6666666666666666)(x +4.0) which is not what I want from it. This code snippet

require "option_parser"


puts "Please enter your variable values assuming the form ax**2 + bx + c ."

puts "a: "
a = gets
exit if a.nil? 
a = a.to_i 

puts "b: "
b = gets
exit if b.nil? 
b = b.to_i

puts "c: "
c = gets
exit if c.nil? 
c = c.to_i

the_gcd = a.gcd(b).gcd(a.gcd(c))

if the_gcd != 1

  x1 = (-1*b + (b**2 - 4*a*c) ** 0.5)/(2 * a)
  x2 = (-1*b - (b**2 - 4*a*c) ** 0.5)/(2 * a)

  if x1.to_i - x1 != 0 || x2.to_i - x2 != 0
    puts "The root is not a whole number. Consider grouping for factoring the polynomial."
    if x1 < 0 && x2 < 0
      puts "#{the_gcd}(x +#{-x1})(x +#{-x2})"
    elsif x1 < 0 && x2 > 0
      puts "#{the_gcd}(x +#{-x1})(x #{-x2})"
    elsif x1 > 0 && x2 < 0
      puts "#{the_gcd}(x #{-x1})(x +#{-x2})"
    elsif x1 > 0 && x2 > 0
      puts "#{the_gcd}(x #{-x1})(x #{-x2})"
    end    
    exit
  
  else
    if x1 < 0 && x2 < 0
      puts "#{the_gcd}(x +#{-x1.to_i})(x +#{-x2.to_i})"
      exit
    elsif x1 < 0 && x2 > 0
      puts "#{the_gcd}(x +#{-x1.to_i})(x #{-x2.to_i})"
      exit
    elsif x1 > 0 && x2 < 0
      puts "#{the_gcd}(x #{-x1.to_i})(x +#{-x2.to_i})"
      exit
    elsif x1 > 0 && x2 > 0
      puts "#{the_gcd}(x #{-x1.to_i})(x #{-x2.to_i})"
      exit
    end    
  end
   
  
  
  if (b**2 - 4*a*c) < 0
    puts "No real solution. Imaginary numbers involved in the solution."
    exit
  end

end

#The part below does not utilize GCD

x1 = (-1*b + (b**2 - 4*a*c) ** 0.5)/(2 * a)
x2 = (-1*b - (b**2 - 4*a*c) ** 0.5)/(2 * a)

if x1.to_i - x1 != 0 || x2.to_i - x2 != 0
  puts "The root is not a whole number. Consider grouping for factoring the polynomial."
  if x1 < 0 && x2 < 0
    puts "(x +#{-x1})(x +#{-x2})"
  elsif x1 < 0 && x2 > 0
    puts "(x +#{-x1})(x #{-x2})"
  elsif x1 > 0 && x2 < 0
    puts "(x #{-x1})(x +#{-x2})"
  elsif x1 > 0 && x2 > 0
    puts "(x #{-x1})(x #{-x2})"
  end    
  exit

else
  if x1 < 0 && x2 < 0
    puts "(x +#{-x1.to_i})(x +#{-x2.to_i})"
  elsif x1 < 0 && x2 > 0
    puts "(x +#{-x1.to_i})(x #{-x2.to_i})"
  elsif x1 > 0 && x2 < 0
    puts "(x #{-x1.to_i})(x +#{-x2.to_i})"
  elsif x1 > 0 && x2 > 0
    puts "(x #{-x1.to_i})(x #{-x2.to_i})"
  end    
end
   
if (b**2 - 4*a*c) < 0
    puts "No real solution. Imaginary numbers involved in the solution."
    exit
  end

is part of a larger app that deals with other methods of factoring quadratic polynomials as well. Let these other methods of factoring, such as factoring perfect square trinomials or difference of perfect squares, be outside of the scope of this question. I came across a similar problem in this Python code, that apparently was solved with the use of fractions. Might one utilize fractions in Crystal in order to solve my problem?

0

There are 0 best solutions below