Solver Completing the Square for Quadratics
Algebra
->
Finance
-> Solver Completing the Square for Quadratics
Log On
Algebra in Finance
Algebra
Solvers
Solvers
Lessons
Lessons
Answers archive
Answers
Source code of 'Completing the Square for Quadratics'
This Solver (Completing the Square for Quadratics)
was created by by
Shin123(626)
:
View Source
,
Show
,
Put on YOUR site
About Shin123
:
Just a kid who solves math problems for fun :)
==section input Complete the square for the quadratic *[input a=1]x<sup>2</sup>+*[input b=-5]x+*[input c=6]=0. If you want all solutions, including imaginary solutions, toggle imaginary solutions on *[choice sol real-solutions imaginary-solutions] <---------- ==section solution perl $X=x; if ($b<0) { $bb=-$b; $sign="-"; } else { $bb=$b; $sign="+"; } if ($c<0) { $cc=-$c; $signa="-"; } else { $cc=$c; $signa="+"; } if ($a == 1) { # if ($b<0) { #$bb=-$b; # $sign="-"; # } else { # $bb=$b; #$sign="+"; # } # if ($c<0) { # $cc=-$c; #$signa="-"; # } else { #$cc=$c; # $signa="+"; # } print("To complete the square for the quadratic {{{x^2$sign$bb*x$signa$cc=0}}}, we must first find a square which when expanded, has x<sup>2</sup> and $b$X in it.\n\n"); $b1 = $b/2; if ($b1<0) { $signb1="-"; $b1b1=-$b1; } else { $signb1="+"; $b1b1=$b1; } $b4=$c-($b1 ** 2); if ($b4<0) { $b4b4=-$b4; $signb4="-"; } else { $b4b4=$b4; $signb4="+"; } print("{{{(x$signb1$b1b1)^2}}} is the square we are looking for. Expanding {{{(x$signb1$b1b1)^2}}} gets {{{x^2$sign$bb*x+" . ($b1 ** 2) . "}}}. So we have {{{x^2$sign$bb*x+$c=(x$signb1$b1b1)^2$signb4$b4b4}}}. So completing the square gives {{{x^2$sign$bb*x+$c=highlight((x$signb1$b1b1)^2$signb4$b4b4)}}}.\n "); #$b4 = $c-($b1 ** 2); if (-($b4) < 0) { print("Subtracting $b4 from both sides, we get {{{(x$signb1$b1b1)^2=-$b4}}}. Since the right side is negative, there are no real solutions."); if ($sol eq "imaginary-solutions") { print(" However, there are two imaginary solutions."); $q1=$b4; $q2=$q1 ** 0.5; $q3=-$b1; print("Taking the square root of both sides gives {{{system(x$signb1$b1b1= sqrt($q1)*i=$q2*i,x$signb1$b1b1 = -sqrt($q1)*i=-$q2*i)}}}. The two solutions are {{{system($q3+$q2*i,$q3-$q2*i)}}} "); } } elsif(-($b4)>0) { $b5 = -($b4); $b6 = $b5 ** 0.5; $ans1 = $b6-$b1; $ans2 = (-$b6)-$b1; print("Adding $b5 from both sides, we get {{{(x$signb1$b1b1)^2=$b5}}}. Taking the square root of both sides gives {{{system(x$signb1$b1b1=sqrt($b5)=$b6,x$signb1$b1b1=-sqrt($b5)=" . (-($b6)) . ")}}}. {{{system(x=$ans1,x=$ans2)}}} So the solutions are x=" . ($b6-$b1) . " and x=" . ((-$b6)-$b1) . "."); } else { print("So we have the equation {{{(x+$b1)^2=0}}}. There is only one unique solution. x=" . (-($b1)) . "."); } } elsif($a!=0) { print("To complete the square for the quadratic {{{$a*x^2$sign$bb*x$signa$cc=0}}}, we must first find a square which when expanded, has $a$X<sup>2</sup> and $b$X in it.\n\n"); $b2 = $b/$a; $c2 = $c/$a; $b1 = $b2/2; if ($b2<0) { $b2b2=-$b2; $signb2="-"; } else { $b2b2=$b2; $signb2="+"; } if ($c2<0) { $c2c2=-$c2; $signc2="-"; } else { $c2c2=$c2; $signc2="+"; } if ($b1<0) { $b1b1=-$b1; $signb1="-"; } else { $b1b1=$b1; $signb1="+"; } $b4=$c2-($b1 ** 2); if ($b4<0) { $b4b4=-$b4; $signb4="-"; } else { $b4b4=$b4; $signb4="+"; } print("Factoring $a from the left side gives {{{$a(x^2$signb2$b2b2*x$signc2$c2c2)=0}}}. {{{(x$signb1$b1b1)^2}}} is the square we are looking for. So we get {{{$a((x$signb1$b1b1)^2$signb4$b4b4)=0}}}.\n"); $b3 = $a*($c2-($b1 ** 2)); # $b4 = $c2-($b1 ** 2); if ($b3<0) { $b3b3=-$b3; $signb3="-"; } else { $b3b3=$b3; $signb3="+"; } print("Taking the $b4 out of the $a, we get {{{highlight($a(x$signb1$b1b1)^2$signb3$b3b3)}}}."); if (-($b3) < 0) { print(" Subtracting $b3 from both sides, we get {{{$a((x$signb1$b1b1)^2)=-$b3}}}. Since the right side is negative, there are no real solutions."); if ($sol eq "imaginary-solutions") { print(" However, there are two imaginary solutions. Dividing both sides by $a gives {{{(x$signb1$b1b1)^2=" . (-($b3)/$a) . "}}}."); $q1=$b3/$a; $q2=$q1 ** 0.5; $q3=-$b1; print("Taking the square root of both sides gives {{{system(x$signb1$b1b1=sqrt($q1)*i=$q2*i,x$signb1$b1b1=-sqrt($q1)*i=-$q2*i)}}}. The two solutions are {{{system($q3+$q2*i,$q3-$q2*i)}}} "); } } elsif(-($b3)>0) { $b5 = -($b3); $b7 = $b5/$a; $b6 = $b7 ** 0.5; $ans1 = $b6-$b1; $ans2 = (-$b6)-$b1; print(" Adding $b5 to both sides, we get {{{$a((x$signb1$b1b1)^2)=$b5}}}. Dividing both sides by $a gives {{{(x$signb1$b1b1)^2=$b7}}}. "); if($b7<0) { print("Since $b7 is negative, there are no real solutions."); if($sol eq "imaginary-solutions") { $O="i"; print("However, there are two imaginary solutions. Taking the square root of both sides gives {{{system(x+$signb1$b1b1=" . ((-$b7)**0.5) . "*i,x+$signb1$b1b1=-" . ((-$b7)**0.5) . "*i)}}}. So the solutions are {{{-$b1+" . ((-$b7)**0.5) . "$O}}} and {{{-$b1-" . ((-$b7)**0.5) . "$O}}}."); } } else { print("Taking the square root of both sides gives {{{system(x$signb1$b1b1=sqrt($b7)=$b6,x$signb1$b1b1=-sqrt($b7)=" . (-($b6)) . ")}}}. {{{system(x=$ans1,x=$ans2)}}} So the solutions are x=" . ($b6-$b1) . " and x=" . ((-$b6)-$b1) . "."); } } else { print("So we have the equation {{{$a(x$signb1$b1b1)^2=0}}}. Dividing both sides by $a gives {{{(x$signb1$b1b1)^2=0}}}. There is only one unique solution. x=" . (-($b1)) . "."); } } elsif($b!=0) { print "<font color=\"red\" size=\"4\">Since the coefficient of the x^2 term is zero, this is not a quadratic.</font>"; if($b==69) { if($c==42) { print "<font color=\"black\" size=\"1\">nice</font>"; } } } ==section output ==section check ==section practice perl my $a1 = randint(-5,5); while($a1==0) { $a1=randint(-5,5); } my $a2 = randint(-5,5); while($a2==0) { $a2=randint(-5,5); } my $a3 = randint(1,3); $a = $a3; $b = -$a3*($a1+$a2); $c = $a1*$a2*$a3;