reserve i,j,n,n1,n2,m,k,u for Nat,
        r,r1,r2 for Real,
        x,y for Integer,
        a,b for non trivial Nat;

theorem Th27:
  Py(a,n),n are_congruent_mod (a-1)
proof
  set A=a-1;
  A1: a^2 -'1 = a^2 -1 by NAT_1:14,XREAL_1:233;
  consider Fy,Fx be FinSequence of NAT such that
  A2:Sum Fy = Py(a,n) & len Fy = [\ (n+1)/2 /] and
  A3:for i be Nat st 1 <= i <= (n+1)/2 holds
     Fy.i = (n choose (2*i-'1)) * a |^ (n+1-'2*i) * (a^2-'1) |^ (i-'1) and
     a|^n + Sum Fx = Px(a,n) & len Fx = [\ n/2 /] &
       for i be Nat st 1 <= i <= n/2 holds
       Fx.i = (n choose (2*i)) * a |^ (n-'2*i) * (a^2-'1) |^ i  by Th12;
  per cases;
  suppose
A4:   n=0;
    then Py(a,n) = 0 by Th6;
    hence thesis by A4,INT_1:11;
  end;
  suppose
A5:   n <> 0;
    for i be Nat st 1 < i <= len Fy holds (Fy.i) mod A = 0
    proof
      let i be Nat such that
A6:     1 < i <= len Fy;
      i -'1 = i-1 by A6,XREAL_1:233;
      then i-'1 <> 0 by A6;
      then reconsider ii= i-'1-1 as Element of NAT by NAT_1:20;
      i-'1 = ii+1;
      then
A7:    (a^2-'1) |^ (i-'1) = A* (a+1) * ((a^2-'1) |^ ii) by A1,NEWTON:6;
      [\ (n+1)/2 /]<=(n+1)/2 by INT_1:def 6;then
      i <= (n+1)/2 by A2,A6,XXREAL_0:2;
      then Fy.i = (n choose (2*i-'1)) * a |^ (n+1-'2*i) * (a^2-'1) |^ (i-'1)
        by A6,A3
        .=(n choose (2*i-'1)) * a |^ (n+1-'2*i) * (a+1) * ((a^2-'1) |^ ii) *A
        by A7;
      then A divides Fy.i by INT_1:def 3;
      hence thesis by INT_1:62;
    end;
    then Py(a,n) mod A = Fy.1 mod A by Th1,A2;
    then (Py(a,n) - Fy.1) mod A =0 by INT_4:22;
    then A divides Py(a,n)-Fy.1 by INT_1:62;
    then
A8:   Py(a,n),Fy.1 are_congruent_mod A by INT_1:def 4;
    n >=1 by A5,NAT_1:14;
    then n+1 >= 1+1 & 1+1=2*1 by XREAL_1:7;
    then
A9:   (n+1)/2>=1 by XREAL_1:77;
    1-'1=1-1 by XREAL_1:233;
    then
A10:  (a^2-'1) |^ (1-'1) = 1 by NEWTON:4;
    2*1-'1 = 2-1 by XREAL_1:233;
    then
A11:  n choose (2*1-'1) = n by A5,NAT_1:14,NEWTON:23;
A12:  Fy.1 = n * (a |^ (n+1-'2*1)) *1 by A9,A10,A11,A3;
A13:  A >= 2-1 by NAT_2:29,XREAL_1:9;
    per cases by A13,XXREAL_0:1;
    suppose A=1;
      then A * (Py(a,n)- n) = Py(a,n)- n;
      hence thesis by INT_1:def 5;
    end;
    suppose
A14:    A >1;
      a = A*1+1;
      then a mod A =1 mod A by PEPIN:10
        .= 1  by A14,PEPIN:5;
      then a |^ (n+1-'2*1) mod A = 1 by A14,PEPIN:35;
      then a |^ (n+1-'2*1),1 are_congruent_mod A by A14,PEPIN:39;
      then Fy.1,1*n are_congruent_mod A by A12,INT_4:11;
      hence thesis by A8,INT_1:15;
    end;
  end;
end;
