intros p q H'; apply le_antisym; apply Zpower_nat_anti_monotone_le;
rewrite H'; auto with zarith.
Qed.
(* To compute the number of digits structurally, we suppose that we know already an upper bound q. So we start from q down to 0 to find the bigger exponent r such that n^(r-1) < v *)
Fixpoint digitAux (v r : Z) (q : positive) {struct q} : nat :=
match q with
| xH => 0
| xI q' =>
match (n * r)%Z with
| r' =>
match (r ?= v)%Z with
| Datatypes.Gt => 0
| _ => S (digitAux v r' q')
end
end
| xO q' =>
match (n * r)%Z with
| r' =>
match (r ?= v)%Z with
| Datatypes.Gt => 0
| _ => S (digitAux v r' q')
end
end
end.
(* As we know that log_n q < log_2 q we can define our function digit*)
Definition digit (q : Z) :=
match q with
| Z0 => 0
| Zpos q' => digitAux (Zabs q) 1 (xO q')
| Zneg q' => digitAux (Zabs q) 1 (xO q')
end.
Hint Unfold digit.

Theorem digitAux1 :
forall p r, (Zpower_nat n (S p) * r)%Z = (Zpower_nat n p * (n * r))%Z.