Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program Sqrt1;
- uses
- CRT;
- const
- eps = 0.000001;
- var
- A, L, R, c, MediumBin: double;
- n: integer;
- NegativeNumber, Error: boolean;
- function BinPow(a: double; n: integer): double;
- var
- b: double;
- begin
- if (n = 0) then
- Result := 1
- else if (n mod 2 = 1) then
- Result := BinPow(a, n - 1) * a
- else
- begin
- b := BinPow(a, n div 2);
- Result := b * b;
- end;
- end;
- function Bisection(L, R: real): double;
- begin
- while (R - L) > eps do
- begin
- c := (L + R) / 2;
- MediumBin := BinPow(c,n) - A;
- if MediumBin = 0 then
- break;
- if ((BinPow(L, n) - A) * MediumBin) < 0 then
- R := c
- else
- L := c;
- end;
- Result := c;
- end;
- begin
- readln(A, n);
- if (A < 0) then
- begin
- if (n mod 2 <> 0) then
- begin
- NegativeNumber := True;
- A *= -1;
- end
- else
- Error := true;
- end;
- L := 0;
- if A > 1 then
- R := A
- else if A < 1 then
- R := 1;
- Bisection(L, R);
- if not Error then begin
- if NegativeNumber then
- writeln(c * -1: 0: 2)
- else
- writeln(c: 0: 2);
- end
- else
- writeln('ERROR');
- Readkey;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement