reserve i,i1,i2,i3,i4,i5,j,r,a,b,x,y for Integer,
  d,e,k,n for Nat,
  fp,fk for FinSequence of INT,
  f,f1,f2 for FinSequence of REAL,
  p for Prime;
reserve fr for FinSequence of REAL;
reserve fr,f for FinSequence of INT;
reserve b,m for Nat;
reserve b for Integer;
reserve m for Integer;
reserve fp for FinSequence of NAT;
reserve a,m for Nat;

theorem Th42:
  p>2 implies Lege(2,p) = (-1)|^((p^2 -'1) div 8)
proof
  set p9 = (p-'1) div 2;
  set I = idseq p9;
  set fp = 2 * I;
  set nn = p div 8;
A1: p>1 by INT_2:def 4;
  then
A2: p-1 = p-'1 by XREAL_1:233;
A3: for d st d in dom fp holds fp.d = 2*d
  proof
    let d;
    assume
A4: d in dom fp;
    then d in dom I by VALUED_1:def 5;
    then d in Seg len I by FINSEQ_1:def 3;
    then
A5: d is Element of Seg p9 by CARD_1:def 7;
    thus fp.d = 2 * I.d by A4,VALUED_1:def 5
      .= 2 * d by A5;
  end;
  for d being Nat st d in dom fp holds fp.d in NAT;
  then reconsider fp as FinSequence of NAT by FINSEQ_2:12;
  set f = fp mod p;
  reconsider f as FinSequence of NAT by FINSEQ_1:102;
  set X = {k where k is Element of NAT:k in rng f & k > p/2};
  set m = card X;
  dom fp = dom I by VALUED_1:def 5;
  then
A6: len fp = len I by FINSEQ_3:29
    .= p9 by CARD_1:def 7;
  set Y = {d where d is Element of NAT:d in dom f & f.d > p/2};
  for x be object st x in Y holds x in dom f
  proof
    let x be object;
    assume x in Y;
    then ex k be Element of NAT st x = k & k in dom f & f.k > p/ 2;
    hence thesis;
  end;
  then Y c= dom f;
  then reconsider Y as finite Subset of NAT by XBOOLE_1:1;
  set Z = seq((p div 4),p9-'(p div 4));
A7: p mod 8 <= 8-1 by INT_1:52,NAT_D:1;
  8 = 2*4;
  then
A8: 2 divides 8;
A9: now
    assume p mod 8 = 0;
    then 8 divides p by PEPIN:6;
    then p = 8 by INT_2:def 4;
    hence contradiction by A8,NAT_4:12;
  end;
  for x being object st x in X holds x in rng f
  proof
    let x be object;
    assume x in X;
    then ex k being Element of NAT st x = k & k in rng f & k > p /2;
    hence thesis;
  end;
  then
A10: X c= rng f;
  then reconsider X as finite set;
  card X is Element of NAT;
  then reconsider m as Element of NAT;
A11: len f = len fp by EULER_2:def 2;
  then
A12: dom f = dom fp by FINSEQ_3:29;
  assume
A13: p>2;
  then 2,p are_coprime by EULER_1:2;
  then
A14: 2 gcd p = 1 by INT_2:def 3;
  then
A15: Lege(2,p) = (-1)|^m by A13,Th41;
  p is odd by A13,PEPIN:17;
  then
A16: p-1 is even by HILBERT3:2;
  then
A17: p9 = ((p-'1)+1) div 2 by A2,NAT_2:26
    .= p div 2 by A1,XREAL_1:235;
  then
A18: f <> {} by A13,A6,A11,NAT_2:13;
  then reconsider U=dom f as non empty finite Subset of NAT;
  2 divides (p-'1) by A16,A2,PEPIN:22;
  then
A19: p-'1 = 2*p9;
A20: for d st d in dom f holds f.d = 2*d
  proof
    let d;
    assume
A21: d in dom f;
    then d<= p9 by A6,A11,FINSEQ_3:25;
    then 2*d<=(p-'1) by A19,XREAL_1:64;
    then 2*d<p by NAT_2:9,XXREAL_0:2;
    hence 2*d = 2*d mod p by NAT_D:24
      .= fp.d mod p by A3,A12,A21
      .= f.d by A12,A21,EULER_2:def 2;
  end;
A22: for d1,d2 be Nat st 1 <= d1 & d1 < d2 & d2 <= len f holds f.d1 < f.d2
  proof
    let d1,d2 be Nat;
    assume that
A23: 1 <= d1 and
A24: d1 < d2 and
A25: d2 <= len f;
     set k1=f.d1;
     set k2=f.d2;
    1 <= d2 by A23,A24,XXREAL_0:2;
    then d2 in dom f by A25,FINSEQ_3:25;
    then
A28: k2 = 2*d2 by A20;
    d1 <= len f by A24,A25,XXREAL_0:2;
    then d1 in dom f by A23,FINSEQ_3:25;
    then k1 = 2*d1 by A20;
    hence thesis by A24,A28,XREAL_1:68;
  end;
  rng f is non empty Subset of NAT by A18;
  then consider n1 be Element of NAT such that
A29: rng f c= Seg n1 \/ {0} by HEYTING3:1;
  reconsider X as finite Subset of NAT by A10,XBOOLE_1:1;
  Z, ((p-'1) div 2)-'(p div 4) are_equipotent by CALCUL_2:6; then
A30: card Z = ((p-'1) div 2)-'(p div 4) by CARD_1:def 2;
  not 0 in rng f
  proof
    assume 0 in rng f;
    then consider n be Nat such that
A31: n in dom f and
A32: f.n = 0 by FINSEQ_2:10;
    2*n =0 by A20,A31,A32;
    hence contradiction by A31,FINSEQ_3:25;
  end;
  then {0} misses rng f by ZFMISC_1:50;
  then rng f c= Seg n1 by A29,XBOOLE_1:73;
  then
ddd: rng f is included_in_Seg by FINSEQ_1:def 13;
  then
A34: Sgm rng f = f by A22,FINSEQ_1:def 14;
A35: X,Y are_equipotent
  proof
    deffunc F(Element of U) = f.$1;
    set YY = {d where d is Element of U:F(d) in X};
A36: now
      let x be set;
      assume x in X;
      then consider y be Element of NAT such that
A37:  y=x and
A38:  y in rng f and
      y > p/2;
      consider d being Nat such that
A39:  d in U and
A40:  f.d = y by A38,FINSEQ_2:10;
      reconsider d as Element of U by A39;
      take d;
      thus x=F(d) by A37,A40;
    end;
A41: Y c= YY
    proof
      let x be object;
      assume x in Y;
      then
A42:  ex d be Element of NAT st d = x & d in dom f & f.d > p/ 2;
      then reconsider x as Element of U;
      f.x in rng f by FUNCT_1:3;
      then F(x) in X by A42;
      hence thesis;
    end;
    now
      let x be object;
      assume x in YY;
      then consider d be Element of U such that
A43:  d = x and
A44:  f.d in X;
      ex k be Element of NAT st k = f.d & k in rng f & k > p/ 2 by A44;
      hence x in Y by A43;
    end;
    then
A45: YY c= Y;
A46: for d1,d2 being Element of U st F(d1) = F(d2) holds d1 = d2
    proof
      let d1,d2 be Element of U;
      f is one-to-one by ddd,A34,FINSEQ_3:92;
      hence thesis;
    end;
    X,YY are_equipotent from FUNCT_7:sch 3(A36,A46);
    hence thesis by A41,A45,XBOOLE_0:def 10;
  end;
  p div 2 < p by INT_1:56;
  then (p div 2) div 2 <= p div 2 by NAT_2:24;
  then
A48: p div (2*2) <= p div 2 by NAT_2:27;
A49: Z c= Y
  proof
    let x be object;
    assume
A50: x in Z;
    then reconsider x as Element of NAT;
A51: x>=(p div 4)+1 by A50,CALCUL_2:1;
    then (p div 4)+x >= (p div 4)+1 by NAT_1:12;
    then
A52: x >= 1 by XREAL_1:6;
    x<=((p-'1) div 2)-'(p div 4)+(p div 4) by A50,CALCUL_2:1;
    then x<=((p-'1) div 2) by A17,A48,XREAL_1:235;
    then
A53: x in dom f by A6,A11,A52,FINSEQ_3:25;
    x > p/4 by A51,INT_1:29,XXREAL_0:2;
    then 2*x > 2*(p/4) by XREAL_1:68;
    then f.x > p/2 by A20,A53;
    hence thesis by A53;
  end;
  now
    let x be object;
A54: p/4 >= [\p/4/] by INT_1:def 6;
    assume x in Y;
    then consider x1 be Element of NAT such that
A55: x1=x and
A56: x1 in dom f and
A57: f.x1 >p/2;
    2*x1>p/2 by A20,A56,A57;
    then x1 > (p/2)/2 by XREAL_1:83;
    then x1 > [\p/4/] by A54,XXREAL_0:2;
    then
A58: x1 >= (p div 4) + 1 by NAT_1:13;
    x1 <= p9 by A6,A11,A56,FINSEQ_3:25;
    then x1 <= (p9-'(p div 4) + (p div 4)) by A17,A48,XREAL_1:235;
    hence x in Z by A55,A58;
  end;
  then Y c= Z;
  then Y = Z by A49,XBOOLE_0:def 10;
  then
A59: m = ((p-'1) div 2)-'(p div 4) by A30,A35,CARD_1:5;
A60: now
    assume p mod 8 = 2;
    then 8 divides (p - 2) by PEPIN:8;
    then 2 divides (p - 2) by A8,INT_2:9;
    then 2 divides -(p-2) by INT_2:10;
    then 2 divides (2 - p);
    then 2 divides p by Th2;
    hence contradiction by A13,NAT_4:12;
  end;
A61: now
    assume p mod 8 = 4;
    then 8 divides (p - 4) by PEPIN:8;
    then 2 divides (p - 4) by A8,INT_2:9;
    then 2 divides -(p - 4) by INT_2:10;
    then
A62: 2 divides (4 - p);
    4 = 2*2;
    then 2 divides 4;
    then 2 divides p by A62,Th2;
    hence contradiction by A13,NAT_4:12;
  end;
A63: now
    assume p mod 8 = 6;
    then 8 divides (p - 6) by PEPIN:8;
    then 2 divides (p - 6) by A8,INT_2:9;
    then 2 divides -(p - 6) by INT_2:10;
    then
A64: 2 divides (6-p);
    6=2*3;
    then 2 divides 6;
    then 2 divides p by A64,Th2;
    hence contradiction by A13,NAT_4:12;
  end;
  p mod 8 = 0 or ... or p mod 8 = 7 by A7;
  then per cases by A9,A60,A61,A63;
  suppose
    p mod 8 = 1;
    then
A65: p=8*nn+1 by NAT_D:2;
    then p-'1 = 2*(4*nn) by A2;
    then
A66: (p-'1) div 2 = 4*nn;
    p div 4 = (4*(2*nn)+1) div 4 by A65
      .= 2*nn+(1 div 4) by NAT_D:61
      .= 2*nn+0 by NAT_D:27;
    then m = 4*nn - 2*nn by A59,A66,XREAL_1:64,233
      .= 2*nn;
    then
A67: Lege(2,p) =((-1)|^2)|^nn by A15,NEWTON:9
      .= (1|^2)|^nn by WSIERP_1:1
      .= 1;
    (p^2 -'1) div 8 = (((8*nn)^2 + 2*(8*nn)) + 1-'1) div 8 by A65
      .= 8*(8*nn^2 + 2*nn) div 8 by NAT_D:34
      .= 8*nn^2 + 2*nn;
    hence (-1)|^((p^2 -'1) div 8) = (-1)|^(2*(4*nn^2 + nn))
      .= ((-1)|^2)|^(4*nn^2 + nn) by NEWTON:9
      .= (1|^2)|^(4*nn^2 + nn) by WSIERP_1:1
      .= Lege(2,p) by A67;
  end;
  suppose
    p mod 8 = 3;
    then
A68: p=8*nn+3 by NAT_D:2;
    then p-'1 = 2*(4*nn+1) by A2;
    then
A69: (p-'1) div 2 = 4*nn+1;
A70: 4*nn>=2*nn by XREAL_1:64;
    p div 4 = (4*(2*nn)+3) div 4 by A68
      .= 2*nn+(3 div 4) by NAT_D:61
      .= 2*nn+0 by NAT_D:27;
    then m = 4*nn+1-2*nn by A59,A69,A70,NAT_1:12,XREAL_1:233
      .= 2*nn+1;
    then
A71: Lege(2,p) = (-1)|^(2*nn)*(-1) by A15,NEWTON:6
      .= ((-1)|^2)|^nn*(-1) by NEWTON:9
      .= (1|^2)|^nn*(-1) by WSIERP_1:1
      .= -1;
    (p^2 -'1) div 8 = (8*(8*nn^2)+8*(6*nn)+3*3-1) div 8 by A68,NAT_1:12
,XREAL_1:233
      .= 8*(8*nn^2+6*nn+1) div 8
      .= 8*nn^2+6*nn+1;
    hence (-1)|^((p^2 -'1) div 8) = (-1)|^(2*(4*nn^2+3*nn))*(-1) by NEWTON:6
      .= ((-1)|^2)|^(4*nn^2+3*nn)*(-1) by NEWTON:9
      .= (1|^2)|^(4*nn^2+3*nn)*(-1) by WSIERP_1:1
      .= Lege(2,p) by A71;
  end;
  suppose
    p mod 8 = 5;
    then
A72: p=8*nn+5 by NAT_D:2;
    then p-'1 = 2*(4*nn+2) by A2;
    then
A73: (p-'1) div 2 = 4*nn+2;
A74: 4*nn>=2*nn by XREAL_1:64;
    p div 4 = (4*(2*nn+1)+1) div 4 by A72
      .= 2*nn+1+(1 div 4) by NAT_D:61
      .= 2*nn+1+0 by NAT_D:27;
    then m = 4*nn+2-(2*nn+1) by A59,A73,A74,XREAL_1:7,233
      .= 2*nn+1;
    then
A75: Lege(2,p) = (-1)|^(2*nn)*(-1) by A15,NEWTON:6
      .= ((-1)|^2)|^nn*(-1) by NEWTON:9
      .= (1|^2)|^nn*(-1) by WSIERP_1:1
      .= -1;
    (p^2 -'1) div 8=(8*(8*nn^2)+8*(10*nn)+25-1) div 8 by A72,NAT_1:12
,XREAL_1:233
      .= 8*(8*nn^2+10*nn+3) div 8
      .= 8*nn^2+10*nn+3;
    hence (-1)|^((p^2 -'1) div 8) = (-1)|^(2*(4*nn^2)+2*(5*nn)+2*1+1)
      .= (-1)|^(2*(4*nn^2+5*nn+1))*(-1) by NEWTON:6
      .= ((-1)|^2)|^(4*nn^2+5*nn+1)*(-1) by NEWTON:9
      .= (1|^2)|^(4*nn^2+5*nn+1)*(-1) by WSIERP_1:1
      .= Lege(2,p) by A75;
  end;
  suppose
    p mod 8 = 7;
    then
A76: p=8*nn+7 by NAT_D:2;
    then p-'1 = 2*(4*nn+3) by A2;
    then
A77: (p-'1) div 2 = 4*nn+3;
A78: 4*nn>=2*nn by XREAL_1:64;
    p div 4 = (4*(2*nn+1)+3) div 4 by A76
      .= 2*nn+1+(3 div 4) by NAT_D:61
      .= 2*nn+1+0 by NAT_D:27;
    then m = 4*nn+3-(2*nn+1) by A59,A77,A78,XREAL_1:7,233
      .= 2*nn+2;
    then
A79: Lege(2,p) = (-1)|^(2*(nn+1)) by A13,A14,Th41
      .= ((-1)|^2)|^(nn+1) by NEWTON:9
      .= (1|^2)|^(nn+1) by WSIERP_1:1
      .= 1;
    (p^2 -'1) div 8=(8*(8*nn^2)+8*(14*nn)+49-1) div 8 by A76,NAT_1:12
,XREAL_1:233
      .= 8*(8*nn^2+14*nn+6) div 8
      .= 8*nn^2+14*nn+6;
    hence (-1)|^((p^2 -'1) div 8) = (-1)|^(2*(4*nn^2+7*nn+3))
      .= ((-1)|^2)|^(4*nn^2+7*nn+3) by NEWTON:9
      .= (1|^2)|^(4*nn^2+7*nn+3) by WSIERP_1:1
      .= Lege(2,p) by A79;
  end;
end;
