Log in

View Full Version : Math Question!



TViYH
05-24-2008, 06:10 PM
Ok. I made a function that uses Sqrt, and a few others. The question is, how, when using other numbers, multiplication, division, and addition, does it come out to be the same result?

Example:


Successfully compiled (61 ms)
Original number : 81
Number after Result := (((Result + Result) + 5) / 2) - 2.5; : 9
Successfully executed


Here's the actual script.

program SqrtEx;

function SqrtEx(num: Integer): Extended;
begin
WriteLn('Original number : ' + IntToStr(num));
Result := Sqrt(num);
Result := (((Result + Result) + 5) / 2) - 2.5;
WriteLn('Number after Result := (((Result + Result) + 5) / 2) - 2.5; : ' + FloatToStr(Result));
end;


begin
SqrtEx(81);
end.

mixster
05-24-2008, 06:18 PM
Algebra my good friend helps explain it.
Result + Result + 5 := 2 * Result + 5;
2 * Result + 5 / 2 := Result + 2.5;
Result + 2.5 - 2.5 := Result;

All in all, this function does nothing other than Sqrt :)

edit:
if nielsie95 = fail then
Writeln('=p');

nielsie95
05-24-2008, 06:20 PM
Result := Sqrt(num);
Result := (((Result + Result) + 5) / 2) - 2.5;

You first calculate the Sqrt of the number. Then you do Number * 2 + 5. When you divide by two, you get the number/2 + 5/2, which is number + 2,5. So when you do number - 2,5 you'll get the same number as what you started with, which is the sqrt. ;)

EDIT: aw, mixster beat me :p

TViYH
05-24-2008, 06:42 PM
Oh. Lol. I know algebra, but I thought the way I had the parenthesis aligned changed it. Here's how I thought it would go:

Let's say the original number was 81.


Result := Sqrt(81);
Result := 9;
((Result + Result) + 5) / 2) - 2.5
(18 + 5) / 2) - 2.5
(23 / 2) - 2.5
11.5 - 2.5
9


Am I right?

mixster
05-24-2008, 06:52 PM
More or less, that's the way it does it.

SKy Scripter
05-24-2008, 06:55 PM
This is how i calculate Sqrt

works up to a 1000...


program new;
var Pn: array [1..1000] of Extended;


procedure LoadSqrt;
var i: Integer;
begin
for i := 1 to 1000 do
pn[i] := I * I;
end;


function sqrt2(x: Extended): Extended;
var I, N: Integer;
e1, e2, e3: extended;
begin
for i:= 1 to 1000 do
if (pn[i] > x) then
begin
n := i;
break;
end;

e1 := x / n;
e2 := (e1 + n) div 2;

while (abs(sqr(e2) - x) > 0.0000001) do
begin
e3 := (x / e2)
e2 := (e2 + e3) / 2;
end;

Result := e2;
end;


var k: Integer;
begin
LoadSqrt;
for k:= 1 to 100 do
Writeln(Floattostr(sqrt2(k)));

end.