
theorem Th46:
  for n being Element of NAT, p being Prime st n <> 0
  ex f being FinSequence of NAT st len f = n &
  (for k being Element of NAT st k in dom f holds
   (f.k = 1 iff p|^k divides n) & (f.k = 0 iff not p|^k divides n)) & p |-count
  n = Sum f
proof
  let n be Element of NAT;
  let p be Prime;
  defpred P[Nat,object] means ($2=1 iff p|^$1 divides n) & ($2=0 iff not p|^$1
  divides n);
  set f1 = ((p |-count n) |-> 1) ^ ((n -' p|-count n) |-> 0);
  set n1 = p |-count n;
  set n2 = n -' p|-count n;
  set fa=(p |-count n) |-> 1;
  set fb=(n -' p|-count n) |-> 0;
A1: Sum f1 = (Sum fa) + Sum fb by RVSUM_1:75
    .= (p |-count n)*1 + Sum fb by RVSUM_1:80
    .= p |-count n + (n -' p|-count n)*0 by RVSUM_1:80;
  assume
A2: n<>0;
A3: now
    p<>1 by INT_2:def 4;
    then p|^(p |-count n) divides n by A2,NAT_3:def 7;
    then
A4: p |^ (p |-count n) <= n by A2,NAT_D:7;
A5: p>1 by INT_2:def 4;
    assume n < p |-count n;
    then p to_power n < p to_power (p |-count n) by A5,POWER:39;
    then p |^ n < p to_power (p |-count n) by POWER:41;
    then p |^ n < p |^ (p |-count n) by POWER:41;
    then p |^ n < n by A4,XXREAL_0:2;
    hence contradiction by A5,Th3;
  end;
A6: for k being Nat st k in Seg n ex x being object st P[k,x]
  proof
    let k be Nat;
    assume k in Seg n;
    per cases;
    suppose
A7:   p|^k divides n;
      set x = 1;
      take x;
      thus thesis by A7;
    end;
    suppose
A8:   not p|^k divides n;
      set x = 0;
      take x;
      thus thesis by A8;
    end;
  end;
  consider f being FinSequence such that
A9: dom f = Seg n and
A10: for k being Nat st k in Seg n holds P[k,f.k] from FINSEQ_1:sch 1(A6
  );
  now
    let x be object;
    assume x in rng f;
    then consider k being object such that
A11: k in dom f and
A12: f.k=x by FUNCT_1:def 3;
    reconsider k as Element of NAT by A11;
    P[k,f.k] by A9,A10,A11;
    hence x in NAT by A12;
  end;
  then rng f c= NAT;
  then reconsider f as FinSequence of NAT by FINSEQ_1:def 4;
A13: len f1 = len(n1 |-> 1)+len(n2 |-> 0) by FINSEQ_1:22
    .= n1 + len(n2 |-> 0) by CARD_1:def 7
    .= n1 + n2 by CARD_1:def 7
    .= p |-count n + (n - p |-count n) by A3,XREAL_1:233;
A14: for x being object st x in dom f holds f.x=f1.x
  proof
    set ff2=n2 |-> 0;
    set ff1=n1 |-> 1;
    let x be object;
    assume
A15: x in dom f;
    then reconsider x1 = x as Element of NAT;
A16: x in dom f1 by A9,A13,A15,FINSEQ_1:def 3;
    per cases by A16,FINSEQ_1:25;
    suppose
A17:  x1 in dom ff1;
      then
A18:  x in Seg n1 by FUNCOP_1:13;
A19:  p |^ x1 divides n
      proof
        per cases;
        suppose
          x1=0;
          then p |^ x1 = 1 by NEWTON:4;
          hence thesis by NAT_D:6;
        end;
        suppose
          x1>0;
          then x1+1>0+1 by XREAL_1:6;
          then x1>=1 by NAT_1:13;
          then
A20:      x1-'1=x1-1 by XREAL_1:233;
          set j=p|-count n;
          set i=x1-'1;
          p<>1 by INT_2:def 4;
          then
A21:      p |^ j divides n by A2,NAT_3:def 7;
          -1+x1<0+x1 & x1 <= p |-count n by A18,FINSEQ_1:1,XREAL_1:6;
          then
A22:      i<p |-count n by A20,XXREAL_0:2;
          i+1 =x1 by A20;
          hence thesis by A22,A21,NAT_3:4;
        end;
      end;
      ff1.x=1 by A18,FUNCOP_1:7;
      then f1.x1=1 by A17,FINSEQ_1:def 7;
      hence thesis by A9,A10,A15,A19;
    end;
    suppose
      ex m being Nat st m in dom ff2 & x1=len ff1 + m;
      then consider m being Nat such that
A23:  m in dom ff2 and
A24:  x=len ff1 + m;
  m in Seg n2 by A23,FUNCOP_1:13;
      then m>=1 by FINSEQ_1:1;
      then m+(p |-count n) > 0+(p |-count n) by XREAL_1:6;
      then
A25:  x1 > p |-count n by A24,CARD_1:def 7;
A26:  not p |^ x1 divides n
      proof
        assume p |^ x1 divides n;
        then
A27:    p |-count (p |^ x1) <= p |-count n by A2,NAT_3:30;
        p>1 by INT_2:def 4;
        hence contradiction by A25,A27,NAT_3:25;
      end;
      ff2.m=0;
      then f1.x=0 by A23,A24,FINSEQ_1:def 7;
      hence thesis by A9,A10,A15,A26;
    end;
  end;
  take f;
  thus len f = n by A9,FINSEQ_1:def 3;
  thus for k being Element of NAT st k in dom f holds (f.k=1 iff p|^k divides
  n) & (f.k=0 iff not p|^k divides n) by A9,A10;
  dom f = dom f1 by A9,A13,FINSEQ_1:def 3;
  hence thesis by A14,A1,FUNCT_1:2;
end;
