|
| 1 | +/- |
| 2 | +Copyright (c) 2020 Kevin Lacker. All rights reserved. |
| 3 | +Released under Apache 2.0 license as described in the file LICENSE. |
| 4 | +Authors: Kevin Lacker |
| 5 | +-/ |
| 6 | + |
| 7 | +import data.nat.digits |
| 8 | + |
| 9 | +/-! |
| 10 | +# IMO 1962 Q1 |
| 11 | +
|
| 12 | +Find the smallest natural number $n$ which has the following properties: |
| 13 | +
|
| 14 | +(a) Its decimal representation has 6 as the last digit. |
| 15 | +
|
| 16 | +(b) If the last digit 6 is erased and placed in front of the remaining digits, |
| 17 | +the resulting number is four times as large as the original number $n$. |
| 18 | +
|
| 19 | +Since Lean does not explicitly express problems of the form "find the smallest number satisfying X", |
| 20 | +we define the problem as a predicate, and then prove a particular number is the smallest member |
| 21 | +of a set satisfying it. |
| 22 | +-/ |
| 23 | + |
| 24 | +open nat |
| 25 | + |
| 26 | +def problem_predicate (n : ℕ) : Prop := |
| 27 | +(digits 10 n).head = 6 ∧ of_digits 10 ((digits 10 n).tail.concat 6) = 4 * n |
| 28 | + |
| 29 | +/-! |
| 30 | +First, it's inconvenient to work with digits, so let's simplify them out of the problem. |
| 31 | +-/ |
| 32 | + |
| 33 | +abbreviation problem_predicate' (c n : ℕ) : Prop := |
| 34 | +n = 10 * c + 6 ∧ 6 * 10 ^ (digits 10 c).length + c = 4 * n |
| 35 | + |
| 36 | +lemma without_digits {n : ℕ} (h1 : problem_predicate n) : |
| 37 | +∃ c : ℕ, problem_predicate' c n := |
| 38 | +begin |
| 39 | + use n / 10, |
| 40 | + cases n, |
| 41 | + { have h2 : ¬ problem_predicate 0, by norm_num [problem_predicate], |
| 42 | + contradiction }, |
| 43 | + { rw [problem_predicate, digits_def' (dec_trivial : 2 ≤ 10) n.succ_pos, |
| 44 | + list.head, list.tail_cons, list.concat_eq_append] at h1, |
| 45 | + split, |
| 46 | + { rw [← h1.left, add_comm, mod_add_div (n+1) 10], }, |
| 47 | + { rw [← h1.right, of_digits_append, of_digits_digits, |
| 48 | + of_digits_singleton, add_comm, mul_comm], }, }, |
| 49 | +end |
| 50 | + |
| 51 | +/-! |
| 52 | +Now we can eliminate possibilities for `(digits 10 c).length` until we get to the one that works. |
| 53 | +-/ |
| 54 | + |
| 55 | +lemma case_0_digit {c n : ℕ} (h1 : (digits 10 c).length = 0) : ¬ problem_predicate' c n := |
| 56 | +begin |
| 57 | + intro h2, |
| 58 | + have h3 : 6 * 10 ^ 0 + c = 6 * 10 ^ (digits 10 c).length + c, by rw h1, |
| 59 | + have h4 : 6 * 10 ^ 0 + c = 4 * (10 * c + 6), by rw [h3, h2.right, h2.left], |
| 60 | + linarith, |
| 61 | +end |
| 62 | + |
| 63 | +lemma case_1_digit {c n : ℕ} (h1 : (digits 10 c).length = 1) : ¬ problem_predicate' c n := |
| 64 | +begin |
| 65 | + intro h2, |
| 66 | + have h3 : 6 * 10 ^ 1 + c = 6 * 10 ^ (digits 10 c).length + c, by rw h1, |
| 67 | + have h4 : 6 * 10 ^ 1 + c = 4 * (10 * c + 6), by rw [h3, h2.right, h2.left], |
| 68 | + have h6 : c > 0, by linarith, |
| 69 | + linarith, |
| 70 | +end |
| 71 | + |
| 72 | +lemma case_2_digit {c n : ℕ} (h1 : (digits 10 c).length = 2) : ¬ problem_predicate' c n := |
| 73 | +begin |
| 74 | + intro h2, |
| 75 | + have h3 : 6 * 10 ^ 2 + c = 6 * 10 ^ (digits 10 c).length + c, by rw h1, |
| 76 | + have h4 : 6 * 10 ^ 2 + c = 4 * (10 * c + 6), by rw [h3, h2.right, h2.left], |
| 77 | + have h5 : c > 14, by linarith, |
| 78 | + linarith |
| 79 | +end |
| 80 | + |
| 81 | +lemma case_3_digit {c n : ℕ} (h1 : (digits 10 c).length = 3) : ¬ problem_predicate' c n := |
| 82 | +begin |
| 83 | + intro h2, |
| 84 | + have h3 : 6 * 10 ^ 3 + c = 6 * 10 ^ (digits 10 c).length + c, by rw h1, |
| 85 | + have h4 : 6 * 10 ^ 3 + c = 4 * (10 * c + 6), by rw [h3, h2.right, h2.left], |
| 86 | + have h5 : c > 153, by linarith, |
| 87 | + linarith |
| 88 | +end |
| 89 | + |
| 90 | +lemma case_4_digit {c n : ℕ} (h1 : (digits 10 c).length = 4) : ¬ problem_predicate' c n := |
| 91 | +begin |
| 92 | + intro h2, |
| 93 | + have h3 : 6 * 10 ^ 4 + c = 6 * 10 ^ (digits 10 c).length + c, by rw h1, |
| 94 | + have h4 : 6 * 10 ^ 4 + c = 4 * (10 * c + 6), by rw [h3, h2.right, h2.left], |
| 95 | + have h5 : c > 1537, by linarith, |
| 96 | + linarith |
| 97 | +end |
| 98 | + |
| 99 | +/-- Putting this inline causes a deep recursion error, so we separate it out. -/ |
| 100 | +lemma helper_5_digit {c : ℤ} (h : 6 * 10 ^ 5 + c = 4 * (10 * c + 6)) : c = 15384 := by linarith |
| 101 | + |
| 102 | +lemma case_5_digit {c n : ℕ} (h1 : (digits 10 c).length = 5) (h2 : problem_predicate' c n) : |
| 103 | + c = 15384 := |
| 104 | +begin |
| 105 | + have h3 : 6 * 10 ^ 5 + c = 6 * 10 ^ (digits 10 c).length + c, by rw h1, |
| 106 | + have h4 : 6 * 10 ^ 5 + c = 4 * (10 * c + 6), by rw [h3, h2.right, h2.left], |
| 107 | + zify at *, |
| 108 | + exact helper_5_digit h4 |
| 109 | +end |
| 110 | + |
| 111 | +/-- |
| 112 | +`linarith` fails on numbers this large, so this lemma spells out some of the arithmetic |
| 113 | +that normally would be automated. |
| 114 | +-/ |
| 115 | +lemma case_more_digits {c n : ℕ} (h1 : (digits 10 c).length ≥ 6) (h2 : problem_predicate' c n) : |
| 116 | + n ≥ 153846 := |
| 117 | +begin |
| 118 | + have h3 : c ≠ 0, |
| 119 | + { intro h4, |
| 120 | + have h5 : (digits 10 c).length = 0, by simp [h4], |
| 121 | + exact case_0_digit h5 h2 }, |
| 122 | + have h6 : 2 ≤ 10, from dec_trivial, |
| 123 | + calc |
| 124 | + n ≥ 10 * c : le.intro h2.left.symm |
| 125 | + ... ≥ 10 ^ (digits 10 c).length : base_pow_length_digits_le 10 c h6 h3 |
| 126 | + ... ≥ 10 ^ 6 : (pow_le_iff_le_right h6).mpr h1 |
| 127 | + ... ≥ 153846 : by norm_num, |
| 128 | +end |
| 129 | + |
| 130 | +/-! |
| 131 | +Now we combine these cases to show that 153846 is the smallest solution. |
| 132 | +-/ |
| 133 | + |
| 134 | +lemma satisfied_by_153846 : problem_predicate 153846 := |
| 135 | +by norm_num [problem_predicate, digits_def', of_digits] |
| 136 | + |
| 137 | +lemma no_smaller_solutions (n : ℕ) (h1 : problem_predicate n) : n ≥ 153846 := |
| 138 | +begin |
| 139 | + cases without_digits h1 with c h2, |
| 140 | + have h3 : (digits 10 c).length < 6 ∨ (digits 10 c).length ≥ 6, by apply lt_or_ge, |
| 141 | + cases h3, |
| 142 | + case or.inr |
| 143 | + { exact case_more_digits h3 h2, }, |
| 144 | + case or.inl |
| 145 | + { interval_cases (digits 10 c).length with h, |
| 146 | + { exfalso, exact case_0_digit h h2 }, |
| 147 | + { exfalso, exact case_1_digit h h2 }, |
| 148 | + { exfalso, exact case_2_digit h h2 }, |
| 149 | + { exfalso, exact case_3_digit h h2 }, |
| 150 | + { exfalso, exact case_4_digit h h2 }, |
| 151 | + { have h4 : c = 15384, from case_5_digit h h2, |
| 152 | + have h5 : n = 10 * 15384 + 6, from h4 ▸ h2.left, |
| 153 | + norm_num at h5, |
| 154 | + exact h5.ge, }, }, |
| 155 | +end |
| 156 | + |
| 157 | +theorem imo1962_q1 : is_least {n | problem_predicate n} 153846 := |
| 158 | +⟨satisfied_by_153846, no_smaller_solutions⟩ |
0 commit comments