(* "prime" logarithms and p-parts. *)
Definition logn p m := if prime p then logn_rec p m m else 0.

(* Specifications: *)
Lemma lognE p m :
logn p m = if [&& prime p, 0 < m & p %| m] then (logn p (m %/ p)).+1 else 0.
Lemma logn_gt0 p n : (0 < logn p n) = (p \in primes n).
Lemma pfactor_dvdnn p n : p ^ logn p n %| n.
Lemma logn_prime p q : prime q -> logn p q = (p == q).
Lemma pfactor_coprime p n :
prime p -> n > 0 -> {m | coprime p m & n = m * p ^ logn p n}.
Lemma pfactorK p n : prime p -> logn p (p ^ n) = n.
Lemma pfactorKpdiv p n : prime p -> logn (pdiv (p ^ n)) (p ^ n) = n.
Lemma ltn_logl p n : 0 < n -> logn p n < n.
Lemma logn_Gauss p m n : coprime p m -> logn p (m * n) = logn p n.
Lemma lognM p m n : 0 < m -> 0 < n -> logn p (m * n) = logn p m + logn p n.
Lemma lognX p m n : logn p (m ^ n) = n * logn p m.
Lemma logn_div p m n : m %| n -> logn p (n %/ m) = logn p n - logn p m.
Lemma dvdn_pfactor p d n : prime p ->
reflect (exists2 m, m <= n & d = p ^ m) (d %| p ^ n).
Lemma prime_decompE n : prime_decomp n = [seq (p, logn p n) | p <- primes n].
(* Some combinatorial formulae. *)
Lemma divn_count_dvd d n : n %/ d = \sum_(1 <= i < n.+1) (d %| i).
Lemma logn_count_dvd p n : prime p -> logn p n = \sum_(1 <= k < n) (p ^ k %| n).
Definition trunc_log p n :=
let fix loop n k :=
if k is k'.+1 then if p <= n then (loop (n %/ p) k').+1 else 0 else 0
in loop n n.