code 41027;
real procedure KENDALLINV(PROB, N, LEFT);
value PROB, N, LEFT; real PROB, N; Boolean LEFT;
begin integer S, G; real P, PCUM;

     if PROB ≤ 0 ∨ PROB ≥ 1 then
     STATAL3 ERROR(“KENDALLINV”, 1, PROB) else
     if N < 0 ∨ ENTIER(N) < N then
     STATAL3 ERROR(“KENDALLINV”, 2, N);
     G:= N × (N - 1) / 2;
     if N = 0 then
           KENDALLINV:= (if LEFT then -1 else +1)
     else if LEFT then
     begin S:= PHINV(PROB) × SQRT(G × (N × 2 + 5) / 9);
         S:= if ABS(S) > G then G × SIGN(S) else
         G + ENTIER(-(G - S) / 2) × 2;
           if PROB < KENDALLPROB(-G, N) then
               KENDALLINV:= -G - 2
           else
           begin PCUM:= KENDALL(S, N);
                   if PCUM ≤ PROB then
                   begin for P:= KENDALLPROB(S + 2, N)
                        while PCUM + P ≤ PROB do
                        begin S:= S + 2; PCUM:= PCUM + P end
                   end else
                   begin for P:= KENDALLPROB(S, N)
                       while PCUM - P > PROB do
                       begin S:= S - 2; PCUM:= PCUM - P end;
                        S:=S-2
                   end; KENDALLINV:= S
           end
     end else
     begin S:= PHINV(1 - PROB) × SQRT(G × (N × 2 + 5) / 9);
         S:= if ABS(S) > G then G × SIGN(S) else
           G - ENTIER((G - S) / 2) × 2;
           if PROB < KENDALLPROB(G, N) then
           KENDALLINV:= G + 2 else
           begin PCUM:= 1 - KENDALL(S - 2, N);
               if PCUM ≤ PROB then
               begin for P:= KENDALLPROB(S - 2, N)
                        while PCUM + P ≤ PROB do
                        begin S:= S - 2; PCUM:= PCUM + P end
                   end else
                   begin for P:= KENDALLPROB(S, ND)
                       while PCUM - P > PROB do
                        begin S:= S + 2; PCUM:= PCUM - P end;
                        S:= S +2
                   end;
                   KENDALLINV:= S
           end
     end
end KENDALLINV;
eop