Zulip Chat Archive
Stream: new members
Topic: digits question
Angela Li (Jun 29 2020 at 22:27):
Hello! I'm trying to show
import data.nat.digits
lemma digits_le_9 (n : ℕ) : ∀ (d ∈ (digits 10 n)), d ≤ 9 := sorry
but I'm not really sure how to work with a list of arbitrary length.
digits_aux_def
splits the list into n % b :: digits_aux b h (n / b)
so long as 0 < n. I thought the n=0 case doesn't really matter since the list would be empty and so I could just show a digit is %b, but it still asks about the remainder of the list digits_aux b h (n / b)
.
Thanks.
Kevin Buzzard (Jun 29 2020 at 23:05):
I guess you do strong induction on n to prove that all the elements of digits_aux b h n
are < n (for any b>=2)
Angela Li (Jun 29 2020 at 23:13):
Okay I shall try that!
Kevin Buzzard (Jun 30 2020 at 11:59):
import data.nat.digits
/-- The digits in the base b expansion of n are all less than b -/
lemma digits_lt_base (b : ℕ) (hb : 2 ≤ b) (n : ℕ) : ∀ d ∈ digits b n, d < b :=
begin
cases b with b, linarith, cases b with b, linarith, clear hb,
apply nat.strong_induction_on n,
clear n,
intro n,
intro IH,
intro d,
intro hd,
unfold digits at hd IH,
have h := digits_aux_def (b+2) (by linarith) n,
cases n with n,
{ cases hd}, -- base b+2 expansion of 0 has no digits
replace h := h (nat.zero_lt_succ n),
rw h at hd,
cases hd,
{ -- zero'th digit
rw hd,
exact n.succ.mod_lt (by linarith),
},
apply IH _ _ d hd,
apply nat.div_lt_self (nat.succ_pos _),
linarith,
end
-- the base 10 digits of a number are all at most 9
lemma digits_le_9 (n : ℕ) : ∀ (d ∈ (digits 10 n)), d ≤ 9 :=
λ d hd, nat.le_of_lt_succ $ digits_lt_base 10 (by norm_num) n _ hd
@Angela Li
Kevin Buzzard (Jun 30 2020 at 12:32):
Angela Li (Jul 11 2020 at 22:27):
Hi. I am trying to show a number will have the same or less digits than a bigger number.
import data.nat.digits
lemma le_digits_len_le {b n m : ℕ} : n ≤ m → (digits b n).length ≤ (digits b m).length := sorry
I have done cases twice to do it for base 0 and base 1, but I am stuck on base >= 2. I thought I could do induction or strong induction on c where m = n + c, but I got stuck. I'm not sure when to use strong induction vs induction (I kind of used it because I didn't know what else to do).
Thanks.
Kevin Buzzard (Jul 11 2020 at 22:29):
This is a great question!
Kevin Buzzard (Jul 11 2020 at 22:30):
You could also prove it was monotonic as a function of b
Kevin Buzzard (Jul 11 2020 at 22:31):
Why not prove that b^length n <= n and n < b^(1 + length n)?
Kevin Buzzard (Jul 11 2020 at 22:32):
warning : I might have made off by one errors.
Angela Li (Jul 11 2020 at 22:32):
I think I was trying to do that and decided to do this first because I got stuck haha.
Kevin Buzzard (Jul 11 2020 at 22:34):
I think you can do these by strong induction on n
Alex J. Best (Jul 11 2020 at 22:36):
If you can prove that (digits b n).length ≤ (digits b (n+1)).length
first as a separate lemma then you could use that (inductively), to show n ≤ n+1 ≤ n + 2.... ≤ m - 1 ≤ m
to that (digits b n).length ≤ (digits b (n+1)).length ≤ (digits b (n+2)).length ≤ ... ≤ (digits b m).length
Reid Barton (Jul 11 2020 at 22:48):
There seem to be some missing lemmas about digits
: namely that it satisfies the hypotheses of the list in digits_of_digits
Reid Barton (Jul 11 2020 at 22:48):
they're also stated in the docstring for digits
Reid Barton (Jul 11 2020 at 22:49):
but as far as I can tell, not proven as lemmas
Kevin Buzzard (Jul 11 2020 at 23:00):
That's a good trick!
Kevin Buzzard (Jul 11 2020 at 23:00):
If you document it, they will come
Angela Li (Jul 11 2020 at 23:02):
(∀ (h : L ≠ list.nil), L.last h ≠ 0)
Does this mean there's no leading 0s in the number? I don't really understand the definition of last.
Kevin Buzzard (Jul 11 2020 at 23:11):
Isn't it the first digit in the expansion (the way humans write it)
Scott Morrison (Jul 12 2020 at 02:04):
Sorry about the incompleteness of digits. I'll admit I wrote it thinking that the corresponding Freek problem was easy low-hanging fruit, but then got a bit bored of the subject before doing a thorough job.
This is a certainly a place where someone learning Lean could add lots of the useful lemmas in a pull request.
Angela Li (Jul 12 2020 at 03:12):
What's the Freek problem?
I think I will be exploring digits more bc I'm not sure if I can do what I want to do with happy numbers w/o more things about digits. Hopefully can contribute...
Scott Morrison (Jul 12 2020 at 03:49):
https://leanprover-community.github.io/100.html#85
Kevin Buzzard (Jul 19 2020 at 16:45):
@maintainers can @Angela Li have push access to non-master mathlib branches? She's written a couple of other lemmas which would be appropriate for data.nat.digits
. Her github userid is snobbydragon
. Thanks.
Rob Lewis (Jul 19 2020 at 16:50):
I just tried but it looks like someone beat me to it
Bryan Gin-ge Chen (Jul 19 2020 at 16:51):
Same. @Angela Li can visit https://github.com/leanprover-community/mathlib/invitations to accept.
Last updated: Dec 20 2023 at 11:08 UTC