Advertisement
Guest User

Untitled

a guest
Feb 18th, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 1.21 KB | None | 0 0
  1. program Sqrt1;
  2.  
  3. uses
  4.   CRT;
  5.  
  6. const
  7.   eps = 0.000001;
  8. var
  9.   A, L, R, c, MediumBin: double;
  10.   n: integer;
  11.   NegativeNumber, Error: boolean;
  12.  
  13.  
  14.   function BinPow(a: double; n: integer): double;
  15.   var
  16.     b: double;
  17.   begin
  18.     if (n = 0) then
  19.       Result := 1
  20.     else if (n mod 2 = 1) then
  21.       Result := BinPow(a, n - 1) * a
  22.     else
  23.     begin
  24.       b := BinPow(a, n div 2);
  25.       Result := b * b;
  26.     end;
  27.   end;
  28.  
  29.   function Bisection(L, R: real): double;
  30.   begin
  31.     while (R - L) > eps do
  32.     begin
  33.       c := (L + R) / 2;
  34.       MediumBin := BinPow(c,n) - A;
  35.       if MediumBin = 0 then
  36.         break;
  37.       if ((BinPow(L, n) - A) * MediumBin) < 0 then
  38.         R := c
  39.       else
  40.         L := c;
  41.     end;
  42.     Result := c;
  43.   end;
  44.  
  45. begin
  46.   readln(A, n);
  47.   if (A < 0) then
  48.   begin
  49.     if (n mod 2 <> 0) then
  50.     begin
  51.       NegativeNumber := True;
  52.       A *= -1;
  53.     end
  54.     else
  55.       Error := true;
  56.   end;
  57.   L := 0;
  58.   if A > 1 then
  59.       R := A
  60.   else if A < 1 then
  61.       R := 1;
  62.   Bisection(L, R);
  63.   if not Error then begin
  64.     if NegativeNumber then
  65.       writeln(c * -1: 0: 2)
  66.     else
  67.       writeln(c: 0: 2);
  68.     end
  69.   else
  70.     writeln('ERROR');
  71.   Readkey;
  72. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement