Zulip Chat Archive
Stream: new members
Topic: convert in base_7
liu (Feb 10 2025 at 05:05):
Now I have a number, say 629. I want to convert it to a number in base - 7 and store each digit of this number in a set. How should I do it?
Johan Commelin (Feb 10 2025 at 05:22):
I think docs#Nat.digits is what you want.
liu (Feb 10 2025 at 06:57):
I still don't understand how to convert a decimal number to a septenary (base - 7) number and get each digit of it. Can you give me an example?
Johan Commelin (Feb 10 2025 at 07:11):
import Mathlib.Data.Nat.Digits
#eval Nat.digits 7 629
-- [6, 5, 5, 1]
liu (Feb 10 2025 at 07:31):
What should I do to convert this list into a set?
import Mathlib
example(set1:Finset ℕ )(set2:Finset ℕ )(set3:Finset ℕ )(n:ℕ )(h0:n=629)(h1:set1=Nat.digits 7 629)(h2:Nat.digits 8 629):{n:ℕ | n ∈ (set1 ∩ set2) }.ncard=3:=by sorry
Moritz Firsching (Feb 10 2025 at 07:53):
there is toFinset
, so you can do something like this:
import Mathlib
example (set1:Finset ℕ )(set2:Finset ℕ )(set3:Finset ℕ )(n:ℕ )(h0:n=629)
(h1:set1 = (Nat.digits 7 629).toFinset)(h2: set2 = (Nat.digits 8 629).toFinset):
{n:ℕ | n ∈ (set1 ∩ set2) }.ncard=3:=by
rw [h1, h2]
have : {n | n = 5 ∨ n = 6 ∨ n = 1} = {5, 6, 1} := by rfl
simp [this]
Moritz Firsching (Feb 10 2025 at 07:55):
but instead of {n:ℕ | n ∈ (set1 ∩ set2) }.ncard
, perhaps you rather want directly (set1 ∩ set2).card
?
Moritz Firsching (Feb 10 2025 at 07:57):
with better spacing, removing superflous assumption, and actually using n=629 it would look like this:
import Mathlib
example (set1 : Finset ℕ) (set2 : Finset ℕ) (n : ℕ) (h0 : n = 629)
(h1 : set1 = (Nat.digits 7 n).toFinset)(h2 : set2 = (Nat.digits 8 n).toFinset):
(set1 ∩ set2).card = 3 := by
simp [h0, h1, h2]
Last updated: May 02 2025 at 03:31 UTC