reserve k,n,m,l,p for Nat;
reserve n0,m0 for non zero Nat;
reserve f for FinSequence;
reserve x,X,Y for set;
reserve f1,f2,f3 for FinSequence of REAL;
reserve n1,n2,m1,m2 for Nat;
reserve I,j for set;
reserve f,g for Function of I, NAT;
reserve J,K for finite Subset of I;

theorem
  Sum(Euler_phi|NatDivisors n0) = n0
proof
  dom Euler_phi = NAT & rng Euler_phi c= REAL by FUNCT_2:def 1;
  then reconsider EU9 = Euler_phi as sequence of  REAL by FUNCT_2:2;
  set X = Seg n0;
  defpred P[set,set] means $2 = {i where i is Element of NAT: ex h being
  Nat st i in X & $1 in NatDivisors n0 & h=$1 & i gcd n0 = n0 / h};
A1: dom Euler_phi = NAT by FUNCT_2:def 1;
A2: for k being Nat st k in X ex x being Element of bool X st P[k
  ,x]
  proof
    let k be Nat;
    set X9 = {i where i is Element of NAT: ex h being Nat st i in X
    & k in NatDivisors n0 & h=k & i gcd n0 = n0 / h};
    for x being object st x in X9 holds x in X
    proof
      let x be object;
      assume x in X9;
      then
      ex i be Element of NAT st i=x & ex h being Nat st i in X &
      k in NatDivisors n0 & h=k & i gcd n0 = n0 / h;
      hence x in X;
    end;
    then reconsider X9 as Element of bool X by TARSKI:def 3;
    assume k in X;
    take X9;
    thus P[k,X9];
  end;
  consider fp be FinSequence of bool X such that
A3: dom fp = X & for k being Nat st k in X holds P[k,fp.k]
  from FINSEQ_1:sch 5(A2);
A4: NatDivisors n0 c= Seg n0 by MOEBIUS1:40;
  then
A5: rng Sgm NatDivisors n0 c= dom fp by A3,FINSEQ_1:def 14;
  then
A6: rng Sgm NatDivisors n0 c= dom Card fp by CARD_3:def 2;
  then reconsider f1 = (Card fp)*Sgm NatDivisors n0 as FinSequence of NAT by
Th9;
A7: NatDivisors n0 c= dom fp by A3,MOEBIUS1:40;
A8: dom((Card fp)*Sgm NatDivisors n0) = dom Sgm NatDivisors n0 by A6,RELAT_1:27
    .= dom(Euler_phi * Sgm NatDivisors n0) by A5,A1,RELAT_1:27,XBOOLE_1:1;
  for k being Element of NAT st k in dom((Card fp)*Sgm NatDivisors n0)
  holds ((Card fp)*Sgm NatDivisors n0).k = (Euler_phi * Sgm NatDivisors n0).k
  proof
    let k be Element of NAT;
    set k9 = (Sgm NatDivisors n0).k;
    set Y = {l where l is Element of NAT : k9,l are_coprime & l >= 1 &
    l <= k9};
    set Z = {i where i is Element of NAT: ex h being Nat st i in X
    & k9 in NatDivisors n0 & h=k9 & i gcd n0 = n0 / h};
    deffunc F(Nat) = $1*n0/k9;
A9: for x being object st x in Y holds x in NAT
    proof
      let x be object;
      assume x in Y;
      then
      ex l be Element of NAT st x=l & k9,l are_coprime & l >= 1 & l
      <= k9;
      hence x in NAT;
    end;
    assume
A10: k in dom((Card fp)*Sgm NatDivisors n0);
    then k in dom Sgm NatDivisors n0 by FUNCT_1:11;
    then k9 in rng Sgm NatDivisors n0 by FUNCT_1:3;
    then
A11: k9 in NatDivisors n0 by FINSEQ_1:def 14;
    then
A12: 1 <= k9 by A3,A7,FINSEQ_1:1;
    k9,1 are_coprime by WSIERP_1:9;
    then 1 in Y by A12;
    then reconsider Y as non empty Subset of NAT by A9,TARSKI:def 3;
    consider f be Function such that
A13: dom f = Y & for d being Element of Y holds f.d = F(d) from
    FUNCT_1:sch 4;
    for y being object holds y in rng f iff y in Z
    proof
      let y be object;
      hereby
        assume y in rng f;
        then consider x be object such that
A14:    x in dom f and
A15:    y = f.x by FUNCT_1:def 3;
        reconsider x as Element of Y by A13,A14;
A16:    0 < k9 by A11;
        k9 divides n0 by A11,MOEBIUS1:39;
        then consider j be Nat such that
A17:    n0 = k9*j by NAT_D:def 3;
        y = F(x) by A13,A15
          .= x*(n0/k9) by XCMPLX_1:74;
        then
A18:    y = x*(j/(k9/k9)) by A17,XCMPLX_1:77
          .= x*(j/1) by A16,XCMPLX_1:60
          .= x*j;
        then reconsider i=y as Element of NAT by ORDINAL1:def 12;
        x in Y;
        then consider l be Element of NAT such that
A19:    x=l and
A20:    k9,l are_coprime and
A21:    l >= 1 and
A22:    l <= k9;
A23:    x*j <= k9*j by A19,A22,XREAL_1:64;
        j<>0 by A17;
        then j+1 > 0+1 by XREAL_1:6;
        then 1 <= j by NAT_1:13;
        then 1*1 <= x*j by A19,A21,XREAL_1:66;
        then
A24:    i in X by A17,A18,A23;
A25:    k9 gcd l = 1 by A20,INT_2:def 3;
        n0/k9 = j/(k9/k9) by A17,XCMPLX_1:77
          .= j/1 by A16,XCMPLX_1:60;
        then i gcd n0 = n0 / k9 by A19,A17,A18,A25,EULER_1:5;
        hence y in Z by A11,A24;
      end;
      assume y in Z;
      then consider i be Element of NAT such that
A26:  i=y and
A27:  ex h being Nat st i in X & k9 in NatDivisors n0 & h
      =k9 & i gcd n0 = n0 / h;
      i gcd n0 divides i by INT_2:def 2;
      then consider x be Nat such that
A28:  i = (i gcd n0)*x by NAT_D:def 3;
A29:  y = x*n0/k9 by A26,A27,A28,XCMPLX_1:74;
      reconsider x as Element of NAT by ORDINAL1:def 12;
A30:  k9<>0 by A27;
A31:  k9*(i gcd n0) = n0/(k9/k9) by A27,XCMPLX_1:81
        .= n0/1 by A30,XCMPLX_1:60;
      then
A32:  k9,x are_coprime by A27,A28,EULER_1:6;
      x<>0 by A27,A28,FINSEQ_1:1;
      then x+1 > 0+1 by XREAL_1:6;
      then
A33:  x >= 1 by NAT_1:13;
      i <= n0 by A27,FINSEQ_1:1;
      then x <= k9 by A27,A28,A31,XREAL_1:68;
      then x in dom f by A13,A32,A33;
      then reconsider x as Element of Y by A13;
      y = f.x by A13,A29;
      hence y in rng f by A13,FUNCT_1:def 3;
    end;
    then
A34: rng f = Z by TARSKI:2;
    for x1,x2 being object st x1 in dom f & x2 in dom f & f.x1 = f.x2 holds
    x1 = x2
    proof
      let x1,x2 be object;
      assume x1 in dom f;
      then reconsider x19=x1 as Element of Y by A13;
      assume x2 in dom f;
      then reconsider x29=x2 as Element of Y by A13;
      assume
A35:  f.x1 = f.x2;
A36:  F(x19) = f.x19 by A13
        .= F(x29) by A13,A35;
      x19*(n0/k9) = x19*n0/k9 by XCMPLX_1:74
        .= x29*(n0/k9) by A36,XCMPLX_1:74;
      hence x1 = x2 by A12,XCMPLX_1:5;
    end;
    then f is one-to-one by FUNCT_1:def 4;
    then
A37: Y,Z are_equipotent by A13,A34,WELLORD2:def 4;
    fp.k9 = Z by A3,A7,A11;
    then card (fp.k9) = card Y by A37,CARD_1:5;
    then (Card fp).k9 = Euler(k9) by A7,A11,CARD_3:def 2;
    then
A38: (Card fp).k9 = Euler_phi.k9 by Def7;
    ((Card fp)*Sgm NatDivisors n0).k = (Card fp).k9 by A10,FUNCT_1:12;
    hence thesis by A8,A10,A38,FUNCT_1:12;
  end;
  then
A39: (Card fp)*Sgm NatDivisors n0 = Euler_phi * Sgm NatDivisors n0 by A8,
PARTFUN1:5;
  set k = len fp;
A40: Func_Seq(Euler_phi,Sgm NatDivisors n0) = Euler_phi*(Sgm NatDivisors n0
  ) by BHSP_5:def 4
    .= Func_Seq(EU9,Sgm NatDivisors n0) by BHSP_5:def 4;
A41: for x being object
holds x in NatDivisors n0 iff x in Seg n0 & not x in (
  Card fp)"{0}
  proof
    reconsider y = 0 as Element of NAT;
    let x be object;
A42: y in {0} by TARSKI:def 1;
    hereby
      assume
A43:  x in NatDivisors n0;
      then reconsider k=x as Element of NAT;
      thus x in Seg n0 by A4,A43;
      assume x in (Card fp)"{0};
      then consider y be object such that
A44:  [x,y] in Card fp & y in {0} by RELAT_1:def 14;
      y = 0 & y = (Card fp).x by A44,FUNCT_1:1,TARSKI:def 1;
      then card (fp.x) = {} by A3,A4,A43,CARD_3:def 2;
      then
A45:  fp.x = {};
      k divides n0 by A43,MOEBIUS1:39;
      then consider i be Nat such that
A46:  n0 = k * i by NAT_D:def 3;
      i <> 0 by A46;
      then 0+1 < i+1 by XREAL_1:6;
      then
A47:  1 <= i by NAT_1:13;
A48:  i divides n0 by A46,NAT_D:def 3;
      then i <= n0 by NAT_D:7;
      then
A49:  i in X by A47;
A50:  k <> 0 by A46;
      n0 = k * (i gcd n0) by A46,A48,NEWTON:49;
      then n0 / k = (i gcd n0) * (k / k) by XCMPLX_1:74
        .= (i gcd n0) * 1 by A50,XCMPLX_1:60;
      then i in {i9 where i9 is Element of NAT: ex h being Nat st
      i9 in X & k in NatDivisors n0 & h=k & i9 gcd n0 = n0 / h} by A43,A49;
      hence contradiction by A3,A4,A43,A45;
    end;
    assume
A51: x in Seg n0;
    then reconsider k=x as Element of NAT;
A52: fp.k = {i9 where i9 is Element of NAT: ex h being Nat st
    i9 in X & k in NatDivisors n0 & h=k & i9 gcd n0 = n0 / h} by A3,A51;
    assume
A53: not x in (Card fp)"{0};
    assume
A54: not x in NatDivisors n0;
    fp.k = {}
    proof
      assume fp.k <> {};
      then consider x9 be object such that
A55:  x9 in fp.k by XBOOLE_0:def 1;
      ex i be Element of NAT st x9 = i & ex h being Nat st i in
      X & k in NatDivisors n0 & h=k & i gcd n0 = n0 / h by A52,A55;
      hence contradiction by A54;
    end;
    then
A56: y = (Card fp).x by A3,A51,CARD_1:27,CARD_3:def 2;
    x in dom Card fp by A3,A51,CARD_3:def 2;
    then [k,y] in Card fp by A56,FUNCT_1:1;
    hence contradiction by A53,A42,RELSET_1:30;
  end;
  reconsider f29=Card fp as FinSequence of REAL by Th13;
  reconsider f19=f1 as FinSequence of REAL by Th13;
  Seg n0 = dom Card fp by A3,CARD_3:def 2;
  then f19 = f29 - {0} by A41,XBOOLE_0:def 5;
  then
A58: Sum f19 = Sum f29 by Th12;
A59: for d,e being Nat st d in dom fp & e in dom fp & d<>e holds fp.d misses
  fp.e
  proof
    let d,e be Nat;
    assume d in dom fp;
    then
A60: fp.d = {i where i is Element of NAT: ex h being Nat st i in
    X & d in NatDivisors n0 & h=d & i gcd n0 = n0 / h} by A3;
    assume e in dom fp;
    then
A61: fp.e = {i where i is Element of NAT: ex h being Nat st i
    in X & e in NatDivisors n0 & h=e & i gcd n0 = n0 / h} by A3;
    assume
A62: d<>e;
    assume not fp.d misses fp.e;
    then fp.d /\ fp.e <> {} by XBOOLE_0:def 7;
    then consider x be object such that
A63: x in fp.d /\ fp.e by XBOOLE_0:def 1;
    x in fp.d by A63,XBOOLE_0:def 4;
    then
A64: ex i1 be Element of NAT st x=i1 & ex h being Nat st i1 in X
    & d in NatDivisors n0 & h=d & i1 gcd n0 = n0 / h by A60;
    x in fp.e by A63,XBOOLE_0:def 4;
    then
    ex i2 be Element of NAT st x=i2 & ex h being Nat st i2 in X
    & e in NatDivisors n0 & h=e & i2 gcd n0 = n0 / h by A61;
    then d = n0 / (n0 / e) by A64,XCMPLX_1:52;
    hence contradiction by A62,XCMPLX_1:52;
  end;
A65: for x being object holds x in union rng fp iff x in X
  proof
    let x be object;
    thus x in union rng fp implies x in X;
    assume
A66: x in X;
    then reconsider i=x as Nat;
    i gcd n0 divides n0 by NAT_D:def 5;
    then consider h be Nat such that
A67: n0 = (i gcd n0) * h by NAT_D:def 3;
A68: 0 < h by A67;
    then 0+1 < h+1 by XREAL_1:6;
    then
A69: 1 <= h by NAT_1:13;
A70: h divides n0 by A67,NAT_D:def 3;
    then
A71: h in NatDivisors n0 by A68;
    set Y = fp.h;
A72: h <> 0 by A67;
    h <= n0 by A70,NAT_D:7;
    then
A73: h in dom fp by A3,A69;
    then
A74: Y in rng fp by FUNCT_1:3;
A75: fp.h = {i9 where i9 is Element of NAT: ex h9 being Nat st
    i9 in X & h in NatDivisors n0 & h9=h & i9 gcd n0 = n0 / h9} by A3,A73;
    n0 / h = (i gcd n0) * (h / h) by A67,XCMPLX_1:74
      .= (i gcd n0) * 1 by A72,XCMPLX_1:60;
    then x in Y by A66,A71,A75;
    hence x in union rng fp by A74,TARSKI:def 4;
  end;
  card union rng fp = Sum Card fp by A59,INT_5:48;
  then
A76: card X = Sum Card fp by A65,TARSKI:2;
  card X = card n0 by FINSEQ_1:55;
  then n0 = Sum Card fp by A76;
  then Sum Func_Seq(Euler_phi,Sgm NatDivisors n0) = n0 by A58,A39,BHSP_5:def 4;
  hence thesis by A40,Th24;
end;
