 reserve i,j, k,v, w for Nat;
 reserve j1,j2, m, n, s, t, x, y for Integer;
 reserve p for odd Prime;
 reserve a for Real;
 reserve b for Integer;

theorem Them1: :: Lagrange lemma
  for p holds
    ex x1,x2,x3,x4, h be Nat st 0 < h & h < p &
    h*p = x1^2 + x2^2 + x3^2 + x4^2
    proof
      let p;
      consider s such that
A1:   2*s = p+1 by ABIAN:11;
      s > 0 by A1;then
      s in NAT by INT_1:3; then
      reconsider s as Nat;
      set f = LAG4SQf(s);
      set g = LAG4SQg(s);
A5:   dom f = Seg len f by FINSEQ_1:def 3 .= Seg s by Def2;
A6:   dom g = Seg len g by FINSEQ_1:def 3 .= Seg s by Def3;
A7:   f is one-to-one by lem1;
A8:   g is one-to-one by lem2;
A9:   rng f misses rng g
      proof
        assume rng f meets rng g; then
        consider y be object such that
A12:    y in rng f & y in rng g by XBOOLE_0:3;
        consider i1 be object such that
A13:    i1 in dom f and
A14:    y = f.i1 by A12,FUNCT_1:def 3;
        consider i2 be object such that
A15:    i2 in dom g and
A16:    y = g.i2 by A12,FUNCT_1:def 3;
        reconsider i1,i2 as Nat by A13,A15;
        reconsider y as Integer by A14;
A17:    y = (i1 - 1)^2 by A13,Def2,A14;
        y = -1 - (i2 - 1)^2 by A15,Def3,A16;
        hence contradiction by A17;
      end;
A19:  card rng (g^f) = p+1
      proof
A20:    card rng f = card dom f by A7,CARD_1:70
          .= card (Seg len f) by FINSEQ_1:def 3 .= card (Seg s) by Def2
          .= s by FINSEQ_1:57;
A21:    card rng g = card dom g by A8,CARD_1:70
          .= card (Seg len g) by FINSEQ_1:def 3 .= card (Seg s) by Def3
          .= s by FINSEQ_1:57;
        card(rng(g^f)) = card (rng g \/ rng f) by FINSEQ_1:31
          .= card rng g +` card rng f by A9,CARD_2:35
          .= card (s +^ s) by CARD_2:def 1,A20,A21
          .= card (s + s) by CARD_2:36 .= p+1 by A1;
        hence thesis;
      end;
A23:  rng(g^f) = rng g \/ rng f by FINSEQ_1:31;
A24:  dom MODMAP_p = INT by FUNCT_2:def 1;
AAA:  rng (g^f) c= INT by RELAT_1:def 19;
      then
A25:  card dom ((MODMAP_p)|rng (g^f)) = p+1 by A19,A24,RELAT_1:62;
A26:  card rng ((MODMAP_p)|rng (g^f)) <= card Segm p by NAT_1:43;
      set s1 = card rng ((MODMAP_p)|rng (g^f));
      set t1 = card dom ((MODMAP_p)|rng (g^f));
      s1 < t1 by A25,A26,NAT_1:13; then
A28:  s1 in { i where i is Nat: i < t1 };
A29:  dom ((MODMAP_p)|rng (g^f)) <> {} by A25;
      set A = dom ((MODMAP_p)|rng (g^f));
      set B = rng ((MODMAP_p)|rng (g^f));
      defpred P[object,object] means
       ex m1 being Element of INT st $1 in A &
         $2=m1 & ((MODMAP_p)|rng (g^f)).$1 = m1;
A30:  card B in card A by A28,AXIOMS:4;
A31:  for x being object st x in A ex y being object st y in B & P[x,y]
      proof
        let x be object;
        assume
A32:    x in A;
        take y = ((MODMAP_p)|rng (g^f)).x;
        y in B by A32,FUNCT_1:3; then
        y in Segm p; then
        y in { i where i is Nat : i < p } by AXIOMS:4; then
        consider j be Nat such that
A36:    y = j & j < p;
        y in INT by A36,ORDINAL1:def 12, NUMBERS:17;
        hence thesis by FUNCT_1:3,A32;
      end;
      consider h be Function of A,B such that
A38:  for x being object st x in A holds P[x,h.x] from FUNCT_2:sch 1(A31);
      consider m1,m2 be object such that
A39:  m1 in A and
A40:  m2 in A and
A41:  m1 <> m2 and
A42:  h.m1 = h.m2 by A29,A30,RELAT_1:42,FINSEQ_4:65;
A43:  P[m1,h.m1] by A38,A39;
A44:  P[m2,h.m2] by A38,A40;
      reconsider m1,m2 as Element of INT by A39,A40;
A46:  ((MODMAP_p)|rng (g^f)).m1 = (MODMAP_p).m1 by A39,FUNCT_1:47
      .= m1 mod p by Def1;
A47:  ((MODMAP_p)|rng (g^f)).m2 = (MODMAP_p).m2 by A40,FUNCT_1:47
       .= m2 mod p by Def1;
A49:  A = dom (MODMAP_p) /\ rng (g^f) by RELAT_1:61
       .= rng (g^f) by AAA,A24,XBOOLE_1:28;
A50:   m1 in rng f implies m2 in rng g
       proof
         assume
A51:     m1 in rng f;
         assume not m2 in rng g; then
         m2 in rng f by A23,A40,A49,XBOOLE_0:def 3;
         hence contradiction by A1,A41,A47,A44,A42,A43,A46,A51,lem4;
       end;
A54:   m1 in rng g implies m2 in rng f
       proof
         assume
A55:     m1 in rng g;
         assume not m2 in rng f; then
         m2 in rng g by A23,A40,A49,XBOOLE_0:def 3;
         hence contradiction by A1,A41,A47,A44,A42,A43,A46,A55,lem5;
       end;
       ex x1, x2, x3,x4,h be Nat st h > 0 & h < p &
       h*p = x1^2 + x2^2 + x3^2 + x4^2
       proof
A60:     p*p" = 1 by XCMPLX_0:def 7;
         per cases by A23,A39,A49,XBOOLE_0:def 3;
         suppose
A61:       m1 in rng f; then
           consider x0 be object such that
A62:       x0 in dom f and
A63:       m1 = f.x0 by FUNCT_1:def 3;
           reconsider x0 as Nat by A62;
A64:       m1 = (x0-1)^2 by Def2,A62,A63;
           consider y0 be object such that
A65:       y0 in dom g and
A66:       m2 = g.y0 by A50,A61,FUNCT_1:def 3;
           reconsider y0 as Nat by A65;
A67:       m2 = -1 - (y0-1)^2 by Def3,A65,A66;
           (m1 - m2) mod p = ((m1 mod p) - (m2 mod p)) mod p by INT_6:7
            .= 0 by A47,A44,A42,A43,A46,NAT_D:26; then
A69:       (m1 - m2) - ((m1 - m2) div p) * p = 0 by INT_1:def 10;
A70:       (m1 - m2) div p > 0 by A64,A67,A69;
           consider x9 be Nat such that
A71:       x0 = x9 and
A72:       1 <= x9 and
A73:       x9 <= s by A5,A62;
A74:       1-1 <= x9 - 1 by A72,XREAL_1:9;
           consider y9 be Nat such that
A75:       y0 = y9 and
A76:       1 <= y9 and
A77:       y9 <= s by A6,A65;
A78:       1-1 <= y9 - 1 by A76,XREAL_1:9;
           x9 - 1 <= s - 1 by A73,XREAL_1:9; then
A80:       (x9 - 1)^2 <= (s - 1)^2 by A74,XREAL_1:66;
           y9-1 <= s -1 by A77,XREAL_1:9; then
           (y9 - 1)^2 <= (s-1)^2 by A78,XREAL_1:66; then
           (x9 - 1)^2 + (y9 - 1)^2 <= (s-1)^2+(s-1)^2 by A80,XREAL_1:7; then
A84:       (x0 - 1)^2 + (y0 - 1)^2 + 1 <= (s-1)^2 + (s-1)^2 + 1
             by A71,A75,XREAL_1:7;
A85:       p^2 = (2*s -1)^2 by A1;
           2*s -2 > 3 -2 by A1,XREAL_1:9,lem3a; then
A86:       (s+1)*(2*s-2) > 0;
A87:       p^2 - ((s-1)^2 + (s-1)^2 + 1) + ((s-1)^2 + (s-1)^2 + 1)
           > 0 + ((s-1)^2 + (s-1)^2 + 1) by A85,A86,XREAL_1:6;
A89:       x0 -1 in NAT by A71,A74,INT_1:3;
A90:       y0 -1 in NAT by A75,A78,INT_1:3;
           set h = (m1 - m2) div p;
           h in NAT by A70,INT_1:3; then
           reconsider h as Nat;
A92:       h > 0 by A69,A64,A67;
           consider x1,x2,x3,x4 be Nat such that
A93:       x1 = x0 -1 and
A94:       x2 = y0 -1 and
A95:       x3 = 1 and
A96:       x4 = 0 by A89,A90;
A97:       (x0-1)^2 + (y0-1)^2 + 1 = x1^2 + x2^2 + x3^2 + x4^2
             by A96,A95,A94,A93;
           h*p < p*p by A69,A64,A67,A84,A87,XXREAL_0:2; then
           h*p*p" < p*p*p" by XREAL_1:68; then
           h*(p*p") < p*(p*p");
           hence thesis by A92,A69,A64,A67,A97,A60;
         end;
         suppose
A101:      m1 in rng g;
           consider x0 be object such that
A102:      x0 in dom f and
A103:      m2 = f.x0 by A54,A101,FUNCT_1:def 3;
           reconsider x0 as Nat by A102;
           consider y0 be object such that
A104:      y0 in dom g and
A105:      m1 = g.y0 by A101,FUNCT_1:def 3;
           reconsider y0 as Nat by A104;
A106:      m1 = -1 - (y0-1)^2 by A104,A105,Def3;
           (m2 - m1) mod p = ((m2 mod p) - (m1 mod p)) mod p by INT_6:7
           .= 0 by A47,A44,A42,A43,A46,NAT_D:26; then
           (m2 - m1) - ((m2 - m1) div p) * p = 0 by INT_1:def 10; then
A109:      (x0-1)^2-(-1-(y0-1)^2)=((m2-m1) div p) * p by A102,A103,A106,Def2;
A110:      (m2 - m1) div p >= 0 by A109;
           consider x9 be Nat such that
A111:      x0 = x9 and
A112:      1 <= x9 and
A113:      x9 <= s by A102,A5;
A114:      1-1 <= x9 - 1 by A112,XREAL_1:9;
           consider y9 be Nat such that
A115:      y0 = y9 and
A116:      1 <= y9 and
A117:      y9 <= s by A6,A104;
A118:      1-1 <= y9 - 1 by A116,XREAL_1:9;
           x9 - 1 <= s - 1 by A113,XREAL_1:9; then
A120:      (x9 - 1)^2 <=(s - 1)^2 by A114,XREAL_1:66;
           y9-1 <= s -1 by A117,XREAL_1:9; then
           (y9 - 1)^2 <=(s-1)^2 by A118,XREAL_1:66; then
           (x9 - 1)^2 + (y9-1)^2 <= (s-1)^2+(s-1)^2 by A120,XREAL_1:7; then
A124:      (x0 - 1)^2 + (y0-1)^2+1<=(s-1)^2+(s-1)^2+1 by A111,A115,XREAL_1:7;
A125:      p^2 = (2*s -1)^2 by A1;
           2*s -2 > 3 -2 by A1,XREAL_1:9,lem3a; then
           (s+1)*(2*s-2) > 0; then
A127:      p^2 - ((s-1)^2 + (s-1)^2 + 1) + ((s-1)^2 + (s-1)^2 + 1)
             > 0 + ((s-1)^2 + (s-1)^2 + 1) by A125,XREAL_1:6;
           set h = (m2 - m1) div p;
           h in NAT by A110,INT_1:3; then
           reconsider h as Nat;
A129:      x0 -1 in NAT by A114,A111,INT_1:3;
A130:      y0 -1 in NAT by INT_1:3, A115,A118;
A132:      h > 0 by A109;
           consider x1,x2,x3,x4 be Nat such that
A133:      x1 = x0 -1 & x2 = y0 -1 & x3 = 1 & x4 = 0 by A129,A130;
A134:      (x0-1)^2 + (y0-1)^2 + 1 = x1^2 + x2^2 + x3^2 + x4^2 by A133;
           h*p < p*p by A109,A124,A127,XXREAL_0:2; then
           h*p*p" < p*p*p" by XREAL_1:68; then
           h*(p*p") < p*(p*p");
           hence thesis by A60,A109,A132,A134;
         end;
       end;
       hence thesis;
     end;
