SRL Archive - Research & Development Lounge

Square Root Symbol Solver


smithsps

2008-05-22 21:17:21

I made this script for a Quadratic Equation Solver that does not use decimal approximation. Hope anyone can use this.
Comments Please!
\\\**Returns as 'Sqrt(Num)' if it cannot work**///

***Function***
function SquareRootSymbol(Num: integer) : string;
var
Check : integer;
Stop : boolean;

begin
repeat
Inc(Check);
if((Check * Check) = Num)then Result := IntToStr(Check);
if((Check * Check) > Num)then Stop := True;
until(Result = IntToStr(Check)) or (Stop = True);
if(Stop)then
begin
Check := Num;
repeat
Dec(Check);
if(Num - ((Num div (Check * Check)) * (Check * Check)) = 0)then Break;
until(Check = 0);
end;
if(Check < 2)then
Result := 'Sqrt('+ IntToStr(Num) + ')';
if(Stop) and (Check > 2)then
Result := IntToStr(Check) + ' Sqrt(' + IntToStr(Num div (Check * Check)) + ')';
end;


***Working Script***

program SqrtS;

function SquareRootSymbol(Num: integer) : string;
var
Check : integer;
Stop : boolean;

begin
repeat
Inc(Check);
if((Check * Check) = Num)then Result := IntToStr(Check);
if((Check * Check) > Num)then Stop := True;
until(Result = IntToStr(Check)) or (Stop = True);
if(Stop)then
begin
Check := Num;
repeat
Dec(Check);
if(Num - ((Num div (Check * Check)) * (Check * Check)) = 0)then Break;
until(Check = 0);
end;
if(Check < 2)then
Result := 'Sqrt('+ IntToStr(Num) + ')';
if(Stop) and (Check > 2)then
Result := IntToStr(Check) + ' Sqrt(' + IntToStr(Num div (Check * Check)) + ')';
end;

begin
ClearDebug;
writeln(SquareRootSymbol(45));
end.

R0b0t1

2008-05-22 22:58:49

I applaud, my good sir.

mixster

2008-05-23 02:22:49

My one's shorter:

function SqrtRound(num: Integer): Integer;
begin
Result := Round(Sqrt(num));
end;
:p
Anyway, that would probably be best in a plugin for larger numbers otherwise it could take a while, otherwise I can't see why it is quadratic and looks like it has a few too many checks. I'm assuming it works though, in which good job as every working script is good :)
Edit:

function SqrtRound(num: Integer):Integer;
var
i: Integer;
begin
for i := 0 to num div 2 + 1 do
if Sqr(i) = num then
begin
Result := i;
Exit;
end
else
if Sqr(i) > num then
Break;
Result := -1;
end;
That works approximately 7ms faster (0.02 ms faster when you remove the Writeln's) and takes up half as much code and returns -1 when it doesn't have an integer Sqrt.

smithsps

2008-05-23 07:26:05

Yet contrast my good friend.

If you try to find the Square Root of 72

Mine Finds: 6 Sqrt(2)

Your first one finds: 8

Your second one -1;


Soooooo HA!

Examine this test script.
program SquareRoot;

function SquareRootSymbol(Num: integer) : string;
var
Temp, ModCheck : integer;
Stop : boolean;

begin
Inc(Temp);
repeat
if((Temp * Temp) = Num)then Result := IntToStr(Temp);
if((Temp * Temp) < Num)then Inc(Temp);
if((Temp * Temp) > Num)then Stop := True;
until(Result = IntToStr(Temp)) or (Stop = True);
if(Stop = True)then
begin
ModCheck := Num;
repeat
Dec(ModCheck);
Temp := ModCheck * ModCheck;
if(Num - ((Num div Temp) * Temp) = 0)then Break;
until(ModCheck = Num);
end;
if(ModCheck = Num) and (Stop = True)then
Result := '0';
if(Stop) and (ModCheck <> Num)then
Result := IntToStr(ModCheck) + ' Sqrt(' + IntToStr(Num div (ModCheck * ModCheck)) + ')';
end;

function SqrtRound(num: Integer):Integer;
var
i: Integer;
begin
for i := 0 to num div 2 + 1 do
if Sqr(i) = num then
begin
Result := i;
Exit;
end
else
if Sqr(i) > num then
Break;
Result := -1;
end;

function SqrtRound1(num: Integer): Integer;
begin
Result := Round(Sqrt(num));
end;

begin
ClearDebug;
writeln(SquareRootSymbol(72));
writeln(IntToStr(SqrtRound1(72)));
writeln(IntToStr(SqrtRound(72)));
end.

mixster

2008-05-23 11:08:01

I thought the whole point was it returned an integer route or none at all. If that's not the case, then I can modify it, but it really does look like you have extra lines. Anyway, your one returns a wrong result anyway. I take it 6 + Sqrt(2) would be added together then squared? If that's the case, then it returns way off as the Sqrt of 72 is approx. 8.5 while 6 + Sqrt(2) gives approx 7.4.

smithsps

2008-05-23 21:55:46

Heres how it works:

Sqrt(72)
Sqrt(72) = Sqrt(36 * 2)
Sqrt([B]36[/B] * 2)

Then whats the square root of 36? = 6
So then you put the sqrt of 36 outside

Sqrt(36 * 2) = 6 Sqrt(2)
6 Sqrt(2)

Since you cannot take the sqrt of 2 you leave it alone.
***Note you can take it will decimal approximation***

So the answer is: 6 Sqrt(2)
***Note no "+" sign****

Please Pm me if you have any questions.

Edit:
Observe this:
program Sqrt;

var
A, B : string;

begin
ClearDebug;
A := FloatToStr(6 * Sqrt(2));
B := FloatToStr(Sqrt(72));
if(A = B)then
writeln(A);
writeln(B);
writeln('6 Sqrt(2) = Sqrt(72)');
end.

Edit2:
Updates Script!