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
  for y,z,a be Nat holds
     y = Py(a,z) & a > 1
  iff ex x,x1,y1,A,x2,y2 be Nat st
    a>1 &
    [x,y] is Pell's_solution of (a^2-'1) &
    [x1,y1] is Pell's_solution of (a^2-'1)&
    y1>=y & A > y & y >= z &
    [x2,y2] is Pell's_solution of (A^2-'1) &
    y2,y are_congruent_mod x1 &
    A,a are_congruent_mod x1 &
    y2,z are_congruent_mod 2*y &
    A,1 are_congruent_mod 2*y &
    y1,0 are_congruent_mod y^2
proof
  let y,z,a be Nat;
  thus y = Py(a,z) & a > 1 implies ex x,x1,y1,A,x2,y2 be Nat st
    a>1 & [x,y] is Pell's_solution of (a^2-'1) &
    [x1,y1] is Pell's_solution of (a^2-'1)& y1>=y & A>y &
    y >= z &
    [x2,y2] is Pell's_solution of (A^2-'1) &
    y2,y are_congruent_mod x1 &
    A,a are_congruent_mod x1 &
    y2,z are_congruent_mod 2*y &
    A,1 are_congruent_mod 2*y &
    y1,0 are_congruent_mod y^2
  proof
    assume
A1:   y=Py(a,z) & a >1;
    then A2:a is non trivial by NAT_2:28;
    set x= Px(a,z);
A3:   a>=1+1 by NAT_1:13,A1;
A4:   a^2-'1 = a^2-1 by XREAL_1:233,NAT_1:14,A1;
    x^2 - (a^2-'1) *y^2 =1 by A1,Th10,A2;
    then
A5:   [x,y] is Pell's_solution of (a^2-'1) & a > 1
      by A1,Lm3;
A6:  y >= z by Th16,A1,A2;
    per cases;
    suppose
A7:     y=0;
      then
A8:     z=0 by A1,A2;
      then
A9:    x=1 by Th6,A2;
      set x1=x,y1=y,y2=y, A =1;
      A^2-'1 = 1-1 by XREAL_1:233;
      then
A10:    1^2-(A^2-'1)*y2^2 = 1;
      1 in INT & 0 in INT by INT_1:def 2;
      then
A11:    [1,0] in [:INT,INT:] by ZFMISC_1:87;
      [1,0]`1 = 1 & [1,0]`2=0;
      then
A12:    [1,y2] is Pell's_solution of (A^2-'1) by A10,A11,A7,PELLS_EQ:def 1;
A13:    y2,y are_congruent_mod x1  by INT_1:11;
A14:    A,a are_congruent_mod x1 by INT_1:13,A9;
A15:    A,1 are_congruent_mod 2*y  by INT_1:11;
      y1,0 are_congruent_mod y^2 by A7,INT_1:11;
      hence thesis by A5,A12,A13,A14,A8,A7,A15;
    end;
    suppose
A16:    y>0;
      reconsider B=(a^2-'1)*((2*y^2)^2) as non square Nat by A2,A16;
      set M = min_Pell's_solution_of B;
      set x1=M`1,Y1=M`2;
A17:    x1^2-B*Y1^2=1 by PELLS_EQ:def 1;
      set y1=2*y^2*Y1;
A18:    x1=[x1,y1]`1 & y1=[x1,y1]`2;
      x1 in INT & y1 in INT by INT_1:def 2;
      then
A19:    [x1,y1] in [:INT,INT:] by ZFMISC_1:87;
A20:    x1^2 - (a^2-'1)*y1^2=
      1+B*Y1^2 - (a^2-'1)*y1^2 by PELLS_EQ:def 1
        .=1;
      then
A21:  [x1,y1] is Pell's_solution of (a^2-'1) by A18,A19,PELLS_EQ:def 1;
      2*Y1*y >=1*1 by A16,NAT_1:14;
      then
A22:  2*Y1*y*y >= 1*y by XREAL_1:66;
A23:  y1 >0 by A22;
      then
A24:  y1^2 >=1 & y1 >=1 by NAT_1:14;
      y1= y^2*(2*Y1) & y1 = (2*y)*(y*Y1);
      then
A25:  y^2 divides y1-0 & 2*y divides y1 by INT_1:def 3;
      then
A26:  y1,0 are_congruent_mod y^2 by INT_1:def 4;
      a^2 >= 2*a & a+a > a+1 by A2,A1,NAT_2:29,XREAL_1:8,64;
      then a^2 > a+1 by XXREAL_0:2;
      then
A27:  a^2-'1>a by A4,XREAL_1:20;
      then
A28:  B > a *1 by A16,NAT_1:14, XREAL_1:97;
      Y1^2 >= 1^2 by NAT_1:14;
      then 1+B*Y1^2 > a by NAT_1:13,A28, XREAL_1:66;
      then x1^2-a >=a-a by A17,XREAL_1:9;
      then reconsider x1a=x1^2-a as Element of NAT by INT_1:3;
      set A = a+x1^2*x1a;
      A >=2+0 by A3,XREAL_1:7;
      then A <>1 & A  is non empty;
      then reconsider A as non trivial Nat by NAT_2:28;
      A-a = x1*(x1*x1a);
      then
A29:    A,a are_congruent_mod x1 by INT_1:def 4,def 3;
      A = a+(1+(a^2-'1)*y1^2)*(1+(a^2-'1)*y1^2-a) by A20;
      then
A30:  A-1 = y1*(y1*( (a^2-'1) + (1+(a^2-'1)*y1^2)*(a^2-'1) -a * (a^2-'1)));
      then
A31:  y1 divides A-1 by INT_1:def 3;
      then
A32:  2*y divides A-1 by A25,INT_2:9;
A33:  A,1 are_congruent_mod 2*y by A31,A25,INT_2:9,INT_1:def 4;
      (a^2-'1)*(y1^2) >= a*1 by A27,A24,XREAL_1:66;
      then 2+ (a^2-'1)*(y1^2) > a*1+0 by XREAL_1:8;
      then 2+(a^2-'1)*y1^2-a >a-a by XREAL_1:9;
      then y1* ((a^2-'1)*(2+(a^2-'1)*y1^2-a)) >= 1*1 by A2,A23,NAT_1:14;
      then A-1 >=1*y1 & A-0 > A-1 by A30,XREAL_1:66,10;
      then A > y1 by XXREAL_0:2;
      then
A34:  A > y by A22, XXREAL_0:2;
      set x2 = Px(A,z),y2 = Py(A,z);
      x2^2 - (A^2-'1) *y2^2 =1 by Th10;
      then
A35:  [x2,y2] is Pell's_solution of (A^2-'1)  by Lm3;
      A-1 divides y2-z by Th27,INT_1:def 4;
      then y2,z are_congruent_mod 2*y by INT_1:def 4,A32,INT_2:9;
      hence thesis by A5,A21,A6,A35,A2,A1,Th29,A29,A33,A26,A22,A34;
    end;
  end;
  given x,x1,y1,A,x2,y2 be Nat such that
A36: a>1 and
A37: [x,y] is Pell's_solution of (a^2-'1) and
A38: [x1,y1] is Pell's_solution of (a^2-'1) and A39: y1 >=y & A > y and
A40: y >= z and
A41: [x2,y2] is Pell's_solution of (A^2-'1) and
A42: y2,y are_congruent_mod x1 and
A43: A,a are_congruent_mod x1 and
A44: y2,z are_congruent_mod 2*y and
A45: A,1 are_congruent_mod 2*y and
A46: y1,0 are_congruent_mod y^2;
A47: a is non trivial by NAT_2:28,A36;
  per cases;
  suppose
A48:  y=0;
    then z=0 by A40;
    hence thesis by A36,A47,Th6,A48;
  end;
  suppose
A49:  y >0;
    then y >=1 by NAT_1:14;
    then reconsider A as non trivial Nat by A39,NAT_2:28;
    consider w be Nat such that
A50:  x = Px(a,w) & y = Py(a,w) by A47,A37,Th7;
    consider u be Nat such that
A51:  x1 = Px(a,u) & y1 = Py(a,u) by A47,A38,Th7;
    consider v be Nat such that
A52:  x2 = Px(A,v) & y2 = Py(A,v) by A41,Th7;
A53: y2,Py(a,v) are_congruent_mod x1 by A47,A43,A52,Th29;
    y,y2 are_congruent_mod x1 by A42,INT_1:14;
    then
A54:  y,Py(a,v) are_congruent_mod Px(a,u) by A51,A53,INT_1:15;
A55:  u<>0 by Th6,A51,A49,A39,A47;
    Py(A,v), v are_congruent_mod 2*y
    proof
A56:    2*y divides A-1 by A45,INT_1:def 4;
      A-1 divides Py(A,v)-v by Th27,INT_1:def 4;
      hence thesis by INT_1:def 4,A56,INT_2:9;
    end;
    then v,y2 are_congruent_mod 2*y by A52,INT_1:14;
    then
A57:  v,z are_congruent_mod 2*y by A44,INT_1:15;
A58:  y^2 divides y1-0 by A46,INT_1:def 4;
A59:  z,v are_congruent_mod 2*y by A57,INT_1:14;
A60:  Py(a,v),Py(a,w) are_congruent_mod Px(a,u) by A54,A50,INT_1:14;
    consider r be Integer such that
A61:  y*r = u by A58,A51,A50,Th40,INT_1:def 3,A47;
A62:  y >= w by A47,A50,Th16;
A63:  0-y <= z-w <= y-0 by A40,A62,XREAL_1:13;
    y +0 < y+y & -y-y < -y -0 by A49,XREAL_1:6,XREAL_1:15;
    then
A64:  (-1)*(2*y) < z-w < 1*(2*y) by A63,XXREAL_0:2;
A65:  0+0 <= z+w <= y+y by A40,A62,XREAL_1:7;
    per cases by A60,A55,Th36,A47;
    suppose v,w are_congruent_mod 2*u;
      then consider t be Integer such that
A66:    v-w = (2*u)*t by INT_1:def 5;
      v-w = (2*y)*(t*r) by A66,A61;
      then v,w are_congruent_mod 2*y by INT_1:def 5;
      then consider i be Integer such that
A67:    z-w = (2*y)*i by INT_1:def 5,A59,INT_1:15;
      -1 < i <0+ 1 by A67,A64,XREAL_1:64;
      then -1+1<= i & i <= 0 by INT_1:7;
      then z-w=0 by A67;
      hence y = Py(a,z) & a > 1 by A36,A50;
    end;
    suppose v,-w are_congruent_mod 2*u;
      then consider t be Integer such that
A68:    v--w = (2*u)*t by INT_1:def 5;
      v--w = (2*y)*(t*r) by A61,A68;
      then v,-w are_congruent_mod 2*y by INT_1:def 5;
      then consider i be Integer such that
A69:    z--w = (2*y)*i by INT_1:def 5,A59,INT_1:15;
      (2*y)* 0 <= (2*y)*i <= (2*y)*1 by A69,A65;
      then
A70:    0 <= i <= 1 by A49,XREAL_1:68;
      i <1 or i=1 by A70,XXREAL_0:1;
      then
A71:    i = 0 or i=1 by A70,NAT_1:14;
      per cases by A71,A69;
      suppose z+w=0;
        then z=0=w;
        hence thesis by A36,A50;
      end;
      suppose
A72:    z+w = 2*y;
A73:    z=y
        proof
          assume z<>y;
          then z < y by A40,XXREAL_0:1;
          then z+w <y+y by A47,A50,Th16,XREAL_1:8;
          hence thesis by A72;
        end;
        w=y
        proof
          assume w<>y;
          then w < y by A62,XXREAL_0:1;
          then z+w <y+y by A40,XREAL_1:8;
          hence thesis by A72;
        end;
        hence thesis by A36,A73,A50;
      end;
    end;
  end;
end;
