const
  PathLength  = 65;
  LineFeed = #10;
  CarriageReturn = #13;
  el_num1=5; {in base circuit}
  el_num2=4; {in reserving circuit}
type
  Data_Array = array[0..9] of real;
  FileName  = string[PathLength];

var
  q:Data_Array;
  Qres,Q_base:real;
  q1,q2,q3,q4,q5,q6,q7,q8,q9:real;
  use:  byte;
  temp_:  Real;
  MainFileName: FileName;
  MainFile: text;

 function Open(var fp:text; name: Filename): boolean;
  begin
    Assign(fp,Name);
    {$I-}
    Reset(fp);
    {$I+}
    Open := IOResult = 0;
 end { Open };

 procedure OpenMain;
  begin
    if ParamCount = 0 then
    begin
      Write('Enter filename: ');
      Readln(MainFileName);
    end
    else
      MainFileName := ParamStr(1);
    if (MainFileName = '') or not Open(MainFile,MainFileName) then
    begin
      Writeln('ERROR:  file not found (', MainFileName, ')');
      Halt(1);
    end;
 end {Open Main};

 procedure ProcessFile;
   begin  {Process File}
    Writeln('Reading.. ');
    use:=0;
    while not EOF(mainfile) do
    begin
      use:=use+1;
      Read(MainFile,q[use]);
      Writeln ('Reading: ',use:3,'>  ',q[use]:6:3);
      if Eoln(MainFile)=true then Readln(MainFile);
    end;
    Writeln('Read ',use,'values.');
    writeln;
    Close(MainFile);
  end {Process File};

 function Base_In (val_:real; base:byte):real;
 var t:byte;temp:real;
  begin
  temp:=val_;
  for t:=1 to base do temp:=temp*val_;
  Base_In:=temp
  end;
 procedure process_data;
 var
     A,B,C,D,E:real;
     k:byte;
  begin
   Q_base:=(1-(1-(1-(1-q[1])*(1-q[3]))*q[2])*(1-q[4]))*q[5];
   Qres:=  (1-(1-(1-(1-q[6])*(1-q[7])))*(1-q[8]))*q[9];
   Writeln('Q of base scheme is ',Q_base:3:5);
   Writeln('Q of reserving scheme is ',Qres:3:5);
   writeln;
   writeln;
   k:=0;
   writeln('Using 1st method.Results are:');
   repeat
    k:=k+1;
    A:= 1 - (1-q[1]) * Base_In ( (1-(1/2)*q[1]) , k );
    B:= 1 - (1-q[3]) * Base_In ( (1-(1/2)*q[3]) , k ) ;
    C:= 1 - (1-q[2]) * Base_In ( (1-(1/2)*q[2]) , k ) ;
    D:= 1 - (1-q[4]) * Base_In ( (1-(1/2)*q[4]) , k ) ;
    E:= 1 - (1-q[5]) * Base_In ( (1-(1/2)*q[5]) , k ) ;
    Q_base:=(1-(1-(1-(1-A)*(1-B))*C)*(1-D))*E;
    writeln('K= ',k:3,' resulting Q_base= ',Q_base:3:6);
    writeln('K= ',k:3,' elements number used: ',el_num1*k:3);
    readln;
   until Q_base>=0.95;
   Q_base:=(1-(1-(1-(1-q[1])*(1-q[3]))*q[2])*(1-q[4]))*q[5];
   Qres:=  (1-(1-(1-(1-q[6])*(1-q[7])))*(1-q[8]))*q[9];
   writeln;
   writeln('Using 2nd method.Results are:');
   writeln;
   k:=0;
   repeat
    k:=k+1;
    Q_base:= 1 - (1 - Q_base) * Base_In ( (1 - Qres ) , k );
    writeln('K= ',k:3,' resulting Q_base= ',Q_base:3:6);
    writeln('K= ',k:3,' elements number used: ',el_num1+(el_num2*k):3);
    readln;
   until Q_base>=0.95

  end;

begin
 OpenMain;
 Writeln('Reading data from file ',MainFileName,' .. Wait..');
 writeln;
 ProcessFile;
 process_data;
 Writeln('It''s all.Press enter to exit.');
 Readln;
end.
