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 Th38:
  Py(a,n*k), k * (Px(a,n) |^ (k-'1)) *Py(a,n) are_congruent_mod Py(a,n)^2
proof
  set m=min_Pell's_solution_of (a^2-'1);
  set x = Px(a,n),y=Py(a,n),s = sqrt (a^2-'1),ys= y*s;
  per cases;
  suppose n=0 or k=0;
    then (y=0 or k=0) & Py(a,n*k)=0 by Th6;
    hence thesis by INT_1:11;
  end;
  suppose A1: n>0 & k >0;
    set I=(x,ys) In_Power k;
A2:  x+ys = (m`1 + m`2 *s) |^n by Def2;
A3:    Px(a,n*k)+Py(a,n*k)*s = (m`1 + m`2 *s) |^(n*k) by Def2
      .= (x+ys) |^ k by A2,NEWTON:9
      .= Sum I by NEWTON:30;
    consider e,o be complex-valued FinSequence such that
A4:   len e = [\len I/2/] & len o = [/len I/2 \] and
A5:   Sum I = Sum e + Sum o and
A6:   for n holds e.n = I.(2*n) & o.n = I.(2*n-1) by Th2;
A7: len I = k+1 by NEWTON:def 4;
    s|^(1+1) = s* s|^1 by NEWTON:6
      .= s^2
      .= (a^2-'1) by SQUARE_1:def 2;
    then
A8:  ys|^2 = (y |^ 2) * (a^2-'1) by NEWTON:7;
    rng o c= NAT
    proof
      let w be object;assume w in rng o;
      then consider z be object such that
A9:    z in dom o & o.z=w by FUNCT_1:def 3;
      reconsider z as Nat by A9;set Z=2*z-1;
A10:    w = I.Z by A6,A9;
A11:    1<= z <= len o by A9,FINSEQ_3:25;
A12:    2*z >=2*1 by XREAL_1:64,A9,FINSEQ_3:25;
      then
A13:    Z >= 2-1 by XREAL_1:9;
      reconsider zz=Z,m = Z-1 as Nat by A12;
A14:    len I/2+1  = (len I+2)/2;
      len o < len I/2 +1 by A4,INT_1:def 7;
      then z < len I/2+1 by A11,XXREAL_0:2;
      then 2*z < k+1+2 & k+1+2 = k+2+1 by A7,A14,XREAL_1:79;
      then
A15:    2*z <= k+1+1 by NAT_1:13;
      then
A16:    Z <= k+1 by XREAL_1:20;
      reconsider l = k+1-zz as Nat by NAT_1:21,XREAL_1:20,A15;
A17:    Z in dom I by A16,A7,A13,FINSEQ_3:25;
      l = k-m;
      then
A18:    I.Z = (k choose m) * x|^l *ys|^m by A17,NEWTON:def 4;
      reconsider z1=z-1 as Nat by A11;
      m = 2*z1;
      then ys|^m = ((y |^ 2) * (a^2-'1)) |^z1 by A8,NEWTON:9;
      hence thesis by A10,A18,ORDINAL1:def 12;
    end;
    then reconsider o as FinSequence of NAT by FINSEQ_1:def 4;
    defpred E[object,object] means $2 in NAT & for n be Nat st n=$2
      holds e.$1 = s * n &
      for N be Nat st N=$1 holds N-1 is Nat & 2*N-1 is Nat & k+1-2*N is Nat
      & for n1,m,l be Nat st n1=N-1 & m= 2*N-1 & l=k+1-2*N holds
      $2 =(k choose m) * x|^l *(((y |^ 2) * (a^2-'1)) |^ n1 * y);
A19:for n st n in Seg len e ex x be object st E[n,x]
    proof
      let n such that
A20:    n in Seg len e;
      set N=2*n;
A21:    1<= n <= len e by A20,FINSEQ_1:1;
      then N >=2*1 by XREAL_1:64;
      then
A22:    N >= 1 by XXREAL_0:2;
      reconsider m = N-1,n1=n-1 as Nat by A21;
      len e <= len I/2 by A4,INT_1:def 6;
      then n <= len I/2 by A21,XXREAL_0:2;
      then
A23:    N <= k+1 by A7,XREAL_1:83;
      then reconsider l = k+1-N as Nat by NAT_1:21;
      take U= (k choose m) * x|^l *(((y |^ 2) * (a^2-'1)) |^ n1 * y);
      thus U in NAT by ORDINAL1:def 12;
      let u be Nat such that
A24:    u=U;
A25:    N in dom I by A23,A7,A22,FINSEQ_3:25;
      l = k-m;
      then
A26:    I.N = (k choose m) * x|^l *ys|^m by A25,NEWTON:def 4;
      m = 2*n1+1;
      then ys|^m = (ys |^(2*n1)) * ys by NEWTON:6
        .= ((y |^ 2) * (a^2-'1)) |^ n1 * ys by A8,NEWTON:9;
      hence e.n = s * u by A6,A26,A24;
      let nn be Nat such that
A27:    n=nn;
      nn-1 =n1 & 2*nn-1 = m & k+1-2*nn = l by A27;
      hence thesis;
    end;
    consider p be FinSequence such that
A28:  dom p = Seg len e & for n st n in Seg len e holds E[n,p.n]
      from FINSEQ_1:sch 1(A19);
    rng p c= NAT
    proof
      let w be object;assume w in rng p;
      then ex n be object st n in dom p & p.n=w by FUNCT_1:def 3;
      hence thesis by A28;
    end;
    then reconsider p as FinSequence of NAT by FINSEQ_1:def 4;
A29:  dom (s*p) = dom p & dom p = dom e
      by A28,VALUED_1:def 5,FINSEQ_1:def 3;
A30:  len p=len e by A28,FINSEQ_1:def 3;
    for n st n in dom p holds e.n = (s*p).n
    proof
      let n;
      assume n in dom p;
      hence e.n = s * (p.n)  by A28
        .= (s*p).n by VALUED_1:6;
    end;
    then e = s*p by A29,FINSEQ_1:13;
    then Sum I = Sum o+s * Sum p by A5,RVSUM_1:87;
    then
A31:  Py(a,n*k) = Sum p by A3,PELLS_EQ:3;
A32:  Sum p is Nat  by TARSKI:1;
A33:  y|^(1+1) = y* y|^1 by NEWTON:6
       .= y*y;
    for n st 1 < n <= len p holds (p.n) mod y^2 =0
    proof
      let n; assume
A34:    1 < n <= len p;
      then
A35:    n in Seg len e by A28,FINSEQ_3:25;
      then E[n,p.n] by A28;
      then reconsider  n1=n-1, m=2*n-1, l= k+1-2*n as Nat;
A36:    p.n = (k choose m) * x|^l *(((y |^ 2) * (a^2-'1)) |^ n1 * y)
        by A35,A28;
A37:    n-1 > 1-1 by A34,XREAL_1:9;
      y^2 divides ((y |^ 2) * (a^2-'1)) |^ n1
        by A37,NEWTON02:14,A33,INT_1:def 3;
      then y^2 divides ((y |^ 2) * (a^2-'1)) |^ n1 * y by NAT_D:9;
      then y^2 divides p.n by A36, NAT_D:9;
      hence thesis by INT_1:62,A1;
    end;
    then Sum p mod y^2 = p.1 mod y^2 by Th1;
    then ((Sum p) - (p.1)) mod (y^2) =0 by A32,INT_4:22,A1;
    then y^2 divides Sum p - p.1 by INT_1:62,A1;
    then
A38:  Sum p, p.1 are_congruent_mod y^2 by INT_1:def 4;
    len I >= 1+1 & 1+1= 2*1 by XREAL_1:6,A7,A1,NAT_1:14;
    then len I/2 >= 1 by XREAL_1:77;
    then len p >= 1 by INT_1:54,A30,A4;
    then
A39:  1 in Seg len e by A28,FINSEQ_3:25;
    then E[1,p.1] by A28;
    then reconsider l= k+1-2*1 as Nat;
A40:  k choose 1 = k by NEWTON:23,A1,NAT_1:14;
A41:  ((y |^ 2) * (a^2-'1)) |^ 0 = 1 by NEWTON:4;
A42:  l=k-1;
    1-1=0 & 2*1-1=1;then
    p.1 = k * x|^l *(1 * y) by A39,A28,A40,A41;
    hence thesis by A42,A31,A38,XREAL_0:def 2;
  end;
end;
