reserve d,i,j,k,m,n,p,q,x,k1,k2 for Nat,
  a,c,i1,i2,i3,i5 for Integer;

theorem Th58:
  (3 |^ ((Fermat(n)-'1) div 2)),(-1) are_congruent_mod Fermat(n)
  implies Fermat(n) is prime
proof
  per cases;
  suppose
    n = 0;
    hence thesis by Th41,Th50;
  end;
  suppose
A1: n > 0;
    Fermat(n) > 2 by Th55;
    then consider p being Element of NAT such that
A2: p is prime and
A3: p divides Fermat(n) by INT_2:31;
    set d = order(3,p);
    consider i being Nat such that
A4: Fermat(n) = p*i by A3,NAT_D:def 3;
A5: p > 2
    proof
      assume p <= 2;
      then p = 2 or p < 1 + 1 by XXREAL_0:1;
      then
A6:   p = 2 or p <= 1 by NAT_1:13;
A7:   p <> 0 by A2,INT_2:def 4;
      Fermat(n) is odd by Lm16;
      then Fermat(n) mod p = 1 by A2,A6,INT_2:def 4,NAT_2:22;
      hence contradiction by A3,A7,Th6;
    end;
A8: Fermat(n) - 1 >= 0;
    assume
A9: (3 |^ ((Fermat(n)-'1) div 2)),(-1) are_congruent_mod Fermat(n);
    then
A10: (3 |^ ((Fermat(n)-'1) div 2))^2,(-1)^2 are_congruent_mod Fermat(n) by
INT_1:18;
    (Fermat(n) -'1) mod 2 = 0 by Lm13;
    then
A11: (3 |^ (Fermat(n)-'1)),(-1)^2 are_congruent_mod Fermat(n) by A10,Th27;
    set 2N = 2 |^ (2 |^ n);
    assume
A12: not Fermat(n) is prime;
    reconsider i as Element of NAT by ORDINAL1:def 12;
A13: 1 < p by A2,INT_2:def 4;
A14: p <> 3
    proof
      1 gcd i = 1 by NEWTON:51;
      then
A15:  3*1 gcd 3*i = 3 by EULER_1:5;
      assume p = 3;
      then not 3,Fermat(n) are_coprime by A4,A15,INT_2:def 3;
      hence contradiction by A1,Th57;
    end;
    then d divides (p-'1) by A2,Th41,Th49,INT_2:30;
    then consider x being Nat such that
A16: p -'1 = d*x by NAT_D:def 3;
A17: (3 |^ ((Fermat(n)-'1) div 2)),(-1) are_congruent_mod p by A9,A4,INT_1:20;
A18: not d divides ((Fermat(n) -'1) div 2)
    proof
      assume d divides ((Fermat(n) -'1) div 2);
      then (3 |^ ((Fermat(n) -'1) div 2)) mod p = 1 by A2,A14,A13,Th41,Th48,
INT_2:30;
      then (3 |^ ((Fermat(n) -'1) div 2)),1 are_congruent_mod p by A13,Th39;
      then 1,-1 are_congruent_mod p by A17,Th40;
      then p divides (1 - -1);
      hence contradiction by A5,NAT_D:7;
    end;
    then
A19: not d divides (2N div 2) by A8,XREAL_0:def 2;
A20: 3,p are_coprime by A2,A14,Th41,INT_2:30;
    then
A21: d <> 0 by A13,Def2;
A22: d > 1
    proof
      assume
A23:  d <= 1;
      now
        per cases by A23,XXREAL_0:1;
        suppose
          d < 1;
          hence thesis by A21,NAT_1:14;
        end;
        suppose
          d = 1;
          hence thesis by A18,NAT_D:6;
        end;
      end;
      hence thesis;
    end;
    3 |^ (Fermat(n)-'1) > 1 by Lm14,Th25;
    then (3 |^ (Fermat(n)-'1)) mod p = 1 by A4,A13,A11,Lm11,INT_1:20;
    then d divides (Fermat(n)-'1) by A20,A13,Th47;
    then d divides 2N by A8,XREAL_0:def 2;
    then d = 2N by A19,A22,Th38,INT_2:28
      .= Fermat(n) -'1 by A8,XREAL_0:def 2;
    then
A24: d = 2N by A8,XREAL_0:def 2;
A25: p*i mod 2N = 1 by A4,Lm15;
    p - 1 > 1 - 1 by A13,XREAL_1:9;
    then
A26: p - 1 = x*d by A16,XREAL_0:def 2;
    then
A27: p = x*2N + 1 by A24;
    then p mod 2N = 1 mod 2N by NAT_D:21
      .= 1 by Lm2,NAT_D:24;
    then (1*i) mod 2N = 1 by A25,EULER_2:8;
    then consider y being Nat such that
A28: i = 2N*y + 1 and
    1 < 2N by NAT_D:def 2;
A29: 2N <> 0 by Lm2;
A30: x >= 1
    proof
      assume x < 1;
      then p = 0*2N + 1 by A27,NAT_1:14;
      hence contradiction by A2,INT_2:def 4;
    end;
    reconsider y as Element of NAT by ORDINAL1:def 12;
    Fermat(n) = (x*2N + 1)*(y*2N + 1) by A4,A24,A26,A28
      .= 2N*(x*y*2N + x + y) + 1;
    then
A31: 1 = 2N*(x*y*2N + x + y) div 2N by A29,Th44
      .= (x*y*2N + x + y) by A29,NAT_D:18;
    2N > 1 by Lm2;
    then p = 1*2N + 1 by A27,A30,A31,Lm1
      .= Fermat(n);
    hence contradiction by A12,A2;
  end;
end;
