reserve n,n1,n2,k,D for Nat,
        r,r1,r2 for Real,
        x,y for Integer;
reserve p,p1,p2 for Pell's_solution of D;

theorem
  for D be non square Nat
     for p be Element of [:INT,INT:] holds
        p is positive Pell's_solution of D
     iff
       ex n be positive Nat st p`1 + p`2 * sqrt D
         =
  ( (min_Pell's_solution_of D)`1 +
    (min_Pell's_solution_of D)`2 *sqrt D ) |^ n
proof
  let D be non square Nat;
  let p be Element of [:INT,INT:];
  set m= min_Pell's_solution_of D,
        t=m`1,u=m`2, S=sqrt D,x=p`1,y=p`2;
  A1: S^2 = D by SQUARE_1:def 2;
  thus p is positive Pell's_solution of D
     implies ex n be positive Nat st x + y*S = (t+u*S)|^n
  proof
    assume A2:p is positive Pell's_solution of D;
    assume A3:for n be positive Nat holds x + y*S <> (t+u*S)|^n;
    ex n st (t+u*S)|^n < x + y*S < (t+u*S)|^(n+1) & n >0
    proof
      set L=[\log(10, x+y*S)/log(10, t+u*S)/];
      A4: x+y*S >1 & t+u*S >1 by A2,Th18;
      (t+u*S)|^1 = t+u*S;
      then A6:x+y*S > t +u*S or t+u*S > x +y*S by XXREAL_0:1,A3;
       x >= t & y >=u by A2,Def3;
      then A7: log(10,1) < log(10,t+u*S)<log(10,x+y*S) & log(10,1)=0
        by A2,A4,A6,Th19,POWER:51,57;
      then 1 < log(10, x+y*S)/log(10, t+u*S) by XREAL_1:187;
      then A8: 1-1 < log(10, x+y*S)/log(10, t+u*S)-1 by XREAL_1:9;
      log(10, x+y*S)/log(10, t+u*S)- 1 < L by INT_1:def 6;
      then reconsider L as Element of NAT by INT_1:3,A8;
      take L;
      (t+u*S)|^L = (t+u*S) to_power L by POWER:41;
      then A10: log(10,(t+u*S)|^L) = L* log(10,t+u*S) by A4,POWER:55;
      A11: 0 < L by A8,INT_1:def 6;
      L * log(10, t+u*S) <= log(10, x+y*S) by XREAL_1:83,A7,INT_1:def 6;
      then (t+u*S)|^L <= x+y*S by A4,A10,POWER:57;
      hence (t+u*S)|^L < x+y*S by A11,A3,XXREAL_0:1;
      (t+u*S)|^(L+1) = (t+u*S) to_power (L+1) by POWER:41;
      then log(10,(t+u*S)|^(L+1)) = (L+1)* log(10,t+u*S) by A4,POWER:55;
      then log(10,(t+u*S)|^(L+1)) > log(10, x+y*S)
        by INT_1:29,XREAL_1:77,A7;
      then (t+u*S)|^(L+1) >= x+y*S by A4,POWER:57;
      hence thesis by A3,XXREAL_0:1,INT_1:def 6,A8;
    end;
    then consider n be Nat such that
    A12: (t+u*S)|^n < x + y*S < (t+u*S)|^(n+1) and A13: n >0;
    consider tn,un be Nat such that
    A14: (t+u*S)|^n = tn + un*S by Th4;
    reconsider TU = [tn,un] as positive Pell's_solution of D
      by A14,A13,Th20;
    A15: TU`1 = tn & TU`2 = un;
    A16: tn^2 - un^2*D = 1 by A15,Lm4;
    then A17: (tn+un*S)*(tn-un*S) =1 by A1;
    tn+un*S > 1 by A15,Th18;
    then tn-un*S > 0 by A17;
    then A18:(t+u*S)|^n *(tn-un*S) < (x + y*S)*(tn-un*S) <
      (t+u*S)|^(n+1) *(tn-un*S)
      by A12,XREAL_1:68;
    A19: 1 < (x + y*S)*(tn-un*S) < (t+u*S)
    proof
      (t+u*S)|^(n+1)*(tn-un*S) = (tn +un*S)*(t+u*S)*(tn-un*S) by A14,NEWTON:6
        .= (tn +un*S)*(tn-un*S)*(t+u*S)
        .= (t+u*S) by A1,A16;
      hence thesis by A18,A16,A1,A14;
    end;
    set a= x*tn-y*un*D, b = y*tn-x*un;
    a^2 -D*b^2 = (x^2 - y^2*D)*(tn-un*S)*(tn+un*S) by A1
      .= 1*(tn-un*S)*(tn+un*S) by A2,Def1
      .= 1 by A1,A16;
    then reconsider ab=[a,b] as Pell's_solution of D by Lm4;
    (x + y*S)*(tn-un*S) = ab`1+ab`2*S by A1;
    then A20: ab is positive by A19,Th18;
    1 < ab`1 + ab`2 * S < t+u*S by A19,A1;
    then ab`1 < t & ab`2 < u by Th19;
    hence thesis by A20,Def3;
  end;
  assume ex n be positive Nat st x + y*S = (t+u*S)|^n;
  then [x,y] is positive Pell's_solution of D by Th20;
  hence thesis;
end;
