//The following code is a program that shows step by step, the deduction of the quadratic formula
export lineByLineFlag := 0;
#cas
deductionQuadFormula_3():=
begin
local ansStr, equstr0, equStr1;
purge(a,b,c,x);
print; // Clear Terminal Window
print( "***** Deduction Quadratic Formula *****" ); // Title
print( "version 3 with intermediate steps & subroutines" );
wait();
choose_cas();
assume(a>0);
print( "Quadratic Equation" );
equ := ((a*x^2+b*x+c) = 0); output:={ equ };
print( ">"+equ );
print( "assume(a>0)" );
pause();
ansStr := "answer * 4*a"; // ((a*x^2+b*x+c)*4*a) = 0
str2expr( ansStr );
pause();
ansStr := "expand( answer )"; // (4*a^2*x^2 +4*a*b*x +4*a*c) = 0
str2expr( ansStr );
pause();
ansStr := "answer + b²"; // (4*a^2*x^2 +4*a*b*x + 4*a*c+b^2 ) = (b^2)
str2expr( ansStr );
pause();
ansStr := "answer - 4*a*c"; // (4*a^2*x^2 +4*a*b*x +4*a*c+b^2 -4*a*c) = (b^2 -4*a*c)
str2expr( ansStr );
pause();
ansStr := "simplify( answer )"; // (4*a^2*x^2 +4*a*b*x +b^2) = (-4*a*c +b^2)
str2expr( ansStr );
pause();
ansStr := "factor( answer )"; // (2*a*x+b)^2) = (-4*a*c+b^2)
str2expr( ansStr );
pause();
ansStr := "√(answer)"; // (abs(2*a*x+b)) = (√(-4*a*c+b^2))
str2expr( ansStr );
pause();
equ := expr( replace( string( equ ), "abs", "" ) ); // (2*a*x+b) = (√(-4*a*c+b^2))
print( string( equ ) );
pause();
ansStr := "answer - b" ; // (2*a*x+b-b) = (√(-4*a*c+b^2)-b)
str2expr( ansStr );
pause();
ansStr := "simplify( answer )"; // (2*a*x-b) = (√(-4*a*c+b^2)-2*b)
str2expr( ansStr );
pause();
ansStr := "answer/( 2*a )"; // (2*a*x/(2*a)) = (-b+√(-4*a*c+b^2))/(2*a)
str2expr( ansStr );
pause();
ansStr := "simplify( answer )";
str2expr( ansStr );
pause();
ansStr := "replace( answer, \"b+\", \"b±\" )"; // x1 = (-b±√[b^2-4*a*c])/(2*a)
print( ">"+ansStr ); equStr0:= replace( ansStr, "answer", "string(" + equ + ")" );
print( ">"+equStr0 );
equStr1 := expr( equStr0 ); print( "" ); print( ""+ equStr1 );
pause();
// ansStr := "expr( replace( string( answer ), \"x\", \"x1\" ))"; // x1 = (-b+√(-4*a*c+b^2))/(2*a)
// str2expr( ansStr );
// pause();
// ansStr := "expr( replace( string( answer ), \"b+\", \"b-\" ))"; // x1 = (-b-√[b^2-4*a*c])/(2*a)
// str2expr( ansStr );
// pause();
// ansStr := "expr( replace( string( answer ), \"x1\", \"x2\" ))"; // x2 = (-b-√(-4*a*c+b^2))/(2*a)
// str2expr( ansStr );
// pause();
purge(a);
return "done";
end;
//
str2expr( ansStr ):=
begin
local equStr0;
print( ">"+ansStr ); output:= append( output, "> "+ansStr);
equStr0:= replace( ansStr, "answer", "(" + equ + ")" ); output:= append( output, "> "+equStr0);
print( ">"+equStr0 );
equ := expr( equStr0 ); output:= append( output, "→ "+equ);
print( "" );
print( ""+ equ );
end;
#end
// Not CAS prg
export pause()
begin
if lineByLineFlag == 1 then
//print( "____________________________[PAUSE]" ); wait( );
print( " [PAUSE]" ); wait( );
else
print( "" );
end;
end;
// Not CAS prg
export choose_cas()
begin
local ok := 1;
local cancel := 0;
local keyPressedOnMenu := 0;
local currentPos := 2;
keyPressedOnMenu := choose(currentPos,"Pause every step", { "No", "Yes" });
if keyPressedOnMenu >= ok then
if currentPos == 2 then
print( "Any key to continue after [PAUSE]" );
print( "" );
lineByLineFlag := 1;
else
print( "Use cursor keys ↑↓ to move the output screen" );
print( "" );
lineByLineFlag:= 0;
end;
else
kill;
end;
end;