2 ||M|| This file is part of HELM, an Hypertextual, Electronic
3 ||A|| Library of Mathematics, developed at the Computer Science
4 ||T|| Department of the University of Bologna, Italy.
7 ||A|| This file is distributed under the terms of the
8 \ / GNU General Public License Version 2
10 V_______________________________________________________________ *)
12 include "arithmetics/nat.ma".
14 let rec mod_aux p m n: nat ≝
17 | S q ⇒ match (leb m n) with
19 | false ⇒ mod_aux q (m-(S n)) n]].
21 definition mod : nat → nat → nat ≝
24 | S p ⇒ mod_aux n n p].
26 interpretation "natural remainder" 'module x y = (mod x y).
28 let rec div_aux p m n : nat ≝
31 | S q ⇒ match (leb m n) with
33 | false ⇒ S (div_aux q (m-(S n)) n)]].
35 definition div : nat → nat → nat ≝
38 | S p ⇒ div_aux n n p].
40 interpretation "natural divide" 'divide x y = (div x y).
42 theorem le_mod_aux_m_m:
43 ∀p,n,m. n ≤ p → mod_aux p n m ≤ m.
45 [ normalize #n #m #lenO @(le_n_O_elim …lenO) //
46 | #q #Hind #n #m #len normalize
47 @(leb_elim n m) normalize //
48 #notlenm @Hind @le_plus_to_minus
49 @(transitive_le … len) /2/
52 theorem lt_mod_m_m: ∀n,m. O < m → n \mod m < m.
55 |#p #_ normalize @le_S_S /2/
59 theorem div_aux_mod_aux: ∀p,n,m:nat.
60 n=(div_aux p n m)*(S m) + (mod_aux p n m).
63 |#q #Hind #n #m normalize
64 @(leb_elim n m) #lenm normalize //
65 >associative_plus <(Hind (n-(S m)) m)
66 applyS plus_minus_m_m (* bello *) /2/
69 theorem div_mod: ∀n,m:nat. n=(n / m)*m+(n \mod m).
70 #n #m (cases m) normalize //
73 theorem eq_times_div_minus_mod:
74 ∀a,b:nat. (a / b) * b = a - (a \mod b).
75 #a #b (applyS minus_plus_m_m) qed.
77 inductive div_mod_spec (n,m,q,r:nat) : Prop ≝
78 div_mod_spec_intro: r < m → n=q*m+r → div_mod_spec n m q r.
80 theorem div_mod_spec_to_not_eq_O:
81 ∀n,m,q,r.div_mod_spec n m q r → m ≠ O.
85 theorem div_mod_spec_div_mod:
86 ∀n,m. O < m → div_mod_spec n m (n / m) (n \mod m).
87 #n #m #posm % /2/ qed.
89 theorem div_mod_spec_to_eq :∀ a,b,q,r,q1,r1.
90 div_mod_spec a b q r → div_mod_spec a b q1 r1 → q = q1.
91 #a #b #q #r #q1 #r1 * #ltrb #spec * #ltr1b #spec1
92 @(leb_elim q q1) #leqq1
93 [(elim (le_to_or_lt_eq … leqq1)) //
94 #ltqq1 @False_ind @(absurd ?? (not_le_Sn_n a))
95 @(lt_to_le_to_lt ? ((S q)*b) ?)
96 [>spec (applyS (monotonic_lt_plus_r … ltrb))
97 |@(transitive_le ? (q1*b)) /2/
99 (* this case is symmetric *)
100 |@False_ind @(absurd ?? (not_le_Sn_n a))
101 @(lt_to_le_to_lt ? ((S q1)*b) ?)
102 [>spec1 (applyS (monotonic_lt_plus_r … ltr1b))
103 |cut (q1 < q) [/2/] #ltq1q @(transitive_le ? (q*b)) /2/
108 theorem div_mod_spec_to_eq2: ∀a,b,q,r,q1,r1.
109 div_mod_spec a b q r → div_mod_spec a b q1 r1 → r = r1.
110 #a #b #q #r #q1 #r1 #spec #spec1
111 cut (q=q1) [@(div_mod_spec_to_eq … spec spec1)]
112 #eqq (elim spec) #_ #eqa (elim spec1) #_ #eqa1
113 @(injective_plus_r (q*b)) //
117 theorem div_mod_spec_times : ∀ n,m:nat.div_mod_spec ((S n)*m) (S n) m O.
118 intros.constructor 1.
119 unfold lt.apply le_S_S.apply le_O_n. demodulate. reflexivity.
120 (*rewrite < plus_n_O.rewrite < sym_times.reflexivity.*)
123 lemma div_plus_times: ∀m,q,r:nat. r < m → (q*m+r)/ m = q.
125 @(div_mod_spec_to_eq … (div_mod_spec_div_mod ???)) /2/
128 lemma mod_plus_times: ∀m,q,r:nat. r < m → (q*m+r) \mod m = r.
130 @(div_mod_spec_to_eq2 … (div_mod_spec_div_mod ???)) /2/
133 (* some properties of div and mod *)
134 theorem div_times: ∀a,b:nat. O < b → a*b/b = a.
136 @(div_mod_spec_to_eq (a*b) b … O (div_mod_spec_div_mod …))
137 // @div_mod_spec_intro // qed.
139 theorem div_n_n: ∀n:nat. O < n → n / n = 1.
142 theorem eq_div_O: ∀n,m. n < m → n / m = O.
144 @(div_mod_spec_to_eq n m (n/m) … n (div_mod_spec_div_mod …))
147 theorem mod_n_n: ∀n:nat. O < n → n \mod n = O.
149 @(div_mod_spec_to_eq2 n n … 1 0 (div_mod_spec_div_mod …))
152 theorem mod_S: ∀n,m:nat. O < m → S (n \mod m) < m →
153 ((S n) \mod m) = S (n \mod m).
155 @(div_mod_spec_to_eq2 (S n) m … (n / m) ? (div_mod_spec_div_mod …))
156 // @div_mod_spec_intro// (applyS eq_f) //
159 theorem mod_O_n: ∀n:nat.O \mod n = O.
162 theorem lt_to_eq_mod: ∀n,m:nat. n < m → n \mod m = n.
164 @(div_mod_spec_to_eq2 n m (n/m) … O n (div_mod_spec_div_mod …))
168 theorem mod_1: ∀n:nat. mod n 1 = O.
169 #n @sym_eq @le_n_O_to_eq
170 @le_S_S_to_le /2/ qed.
172 theorem div_1: ∀n:nat. div n 1 = n.
173 #n @sym_eq napplyS (div_mod n 1) qed. *)
175 theorem or_div_mod: ∀n,q. O < q →
176 ((S (n \mod q)=q) ∧ S n = (S (div n q)) * q ∨
177 ((S (n \mod q)<q) ∧ S n = (div n q) * q + S (n\mod q))).
179 (elim (le_to_or_lt_eq ?? (lt_mod_m_m n q posq))) #H
180 [%2 % // (applyS eq_f) //
181 |%1 % // /demod/ <H in ⊢(? ? ? (? % ?)) @eq_f//
186 theorem injective_times_r:
187 ∀n:nat. O < n → injective nat nat (λm:nat.n*m).
189 <(div_times a n posn) <(div_times b n posn) //
192 theorem injective_times_l:
193 ∀n:nat. O < n → injective nat nat (λm:nat.m*n).
196 (* n_divides computes the pair (div,mod)
197 (* p is just an upper bound, acc is an accumulator *)
198 let rec n_divides_aux p n m acc \def
202 [ O \Rightarrow pair nat nat acc n
203 | (S p) \Rightarrow n_divides_aux p (n / m) m (S acc)]
204 | (S a) \Rightarrow pair nat nat acc n].
206 (* n_divides n m = <q,r> if m divides n q times, with remainder r *)
207 definition n_divides \def \lambda n,m:nat.n_divides_aux n n m O. *)
211 theorem lt_div_S: ∀n,m. O < m → n < S(n / m)*m.
212 #n #m #posm (change with (n < m +(n/m)*m))
213 >(div_mod n m) in ⊢ (? % ?) >commutative_plus
214 @monotonic_lt_plus_l @lt_mod_m_m //
217 theorem le_div: ∀n,m. O < n → m/n ≤ m.
219 >(div_mod m n) in ⊢ (? ? %) @(transitive_le ? (m/n*n)) /2/
222 theorem le_plus_mod: ∀m,n,q. O < q →
223 (m+n) \mod q ≤ m \mod q + n \mod q .
225 (elim (decidable_le q (m \mod q + n \mod q))) #Hle
226 [@(transitive_le … Hle) @le_S_S_to_le @le_S /2/
227 |cut ((m+n)\mod q = m\mod q+n\mod q) //
228 @(div_mod_spec_to_eq2 … (m/q + n/q) ? (div_mod_spec_div_mod … posq)).
231 |>(div_mod n q) in ⊢ (? ? (? ? %) ?)
232 (applyS (eq_f … (λx.plus x (n \mod q))))
233 >(div_mod m q) in ⊢ (? ? (? % ?) ?)
234 (applyS (eq_f … (λx.plus x (m \mod q)))) //
239 theorem le_plus_div: ∀m,n,q. O < q →
240 m/q + n/q \le (m+n)/q.
241 #m #n #q #posq @(le_times_to_le … posq)
242 @(le_plus_to_le_r ((m+n) \mod q))
244 >commutative_times in ⊢ (? ? %) <div_mod
245 >(div_mod m q) in ⊢ (? ? (? % ?)) >(div_mod n q) in ⊢ (? ? (? ? %))
246 >commutative_plus in ⊢ (? ? (? % ?)) >associative_plus in ⊢ (? ? %)
247 <associative_plus in ⊢ (? ? (? ? %)) (applyS monotonic_le_plus_l) /2/
250 theorem le_times_to_le_div: ∀a,b,c:nat.
251 O < b → b*c ≤ a → c ≤ a/b.
253 @le_S_S_to_le @(lt_times_n_to_lt_l b) @(le_to_lt_to_lt ? a)/2/
256 theorem le_times_to_le_div2: ∀m,n,q. O < q →
259 @(le_times_to_le q ? ? posq) @(le_plus_to_le (n \mod q)) /2/
263 theorem lt_m_nm: ∀n,m. O < m → 1 < n → m < n*m.
266 theorem lt_times_to_lt_div: ∀m,n,q. n < m*q → n/q < m.
268 @(lt_times_n_to_lt_l q …) @(lt_plus_to_lt_l (n \mod q)) /2/
272 theorem lt_div: ∀n,m. O < m → 1 < n → m/n < m.
274 @lt_times_to_lt_div (applyS lt_m_nm) //.
277 theorem le_div_plus_S: ∀m,n,q. O < q →
278 (m+n)/q \le S(m/q + n/q).
280 @le_S_S_to_le @lt_times_to_lt_div
281 @(lt_to_le_to_lt … (lt_plus … (lt_div_S m … posq) (lt_div_S n … posq)))
285 theorem le_div_S_S_div: ∀n,m. O < m → (S n)/m ≤ S (n /m).
286 #n #m #posm @le_times_to_le_div2 /2/
289 theorem le_times_div_div_times: ∀a,n,m.O < m →
291 #a #n #m #posm @le_times_to_le_div /2/
294 theorem monotonic_div: ∀n.O < n →
295 monotonic nat le (λm.div m n).
296 #n #posn #a #b #leab @le_times_to_le_div/2/
299 theorem pos_div: ∀n,m:nat. O < m → O < n → n \mod m = O →
301 #n #m #posm #posn #mod0
302 @(lt_times_n_to_lt_l m)// (* MITICO *)
306 theorem lt_div_n_m_n: ∀n,m:nat. 1 < m → O < n → n / m < n.
308 @(leb_elim 1 (n / m))/2/ (* MITICO *)
311 theorem eq_div_div_div_times: ∀n,m,q. O < n → O < m →
314 @(div_mod_spec_to_eq … (q\mod n+n*(q/n\mod m)) … (div_mod_spec_div_mod …)) /2/
315 @div_mod_spec_intro // @(lt_to_le_to_lt ? (n*(S (q/n\mod m))))
316 [(applyS monotonic_lt_plus_l) /2/
317 |@monotonic_le_times_r/2/
321 theorem eq_div_div_div_div: ∀n,m,q. O < n → O < m →
324 @(trans_eq ? ? (q/(n*m)))
326 |@sym_eq (applyS eq_div_div_div_times) //
331 theorem SSO_mod: \forall n,m. O < m \to (S(S O))*n/m = (n/m)*(S(S O)) + mod ((S(S O))*n/m) (S(S O)).
333 rewrite < (lt_O_to_div_times n (S(S O))) in ⊢ (? ? ? (? (? (? % ?) ?) ?))
334 [rewrite > eq_div_div_div_div
335 [rewrite > sym_times in ⊢ (? ? ? (? (? (? (? % ?) ?) ?) ?)).
344 (* Forall a,b : N. 0 < b \to b * (a/b) <= a < b * (a/b +1) *)
345 (* The theorem is shown in two different parts: *)
347 theorem lt_to_div_to_and_le_times_lt_S: \forall a,b,c:nat.
348 O \lt b \to a/b = c \to (b*c \le a \land a \lt b*(S c)).
353 rewrite > eq_times_div_minus_mod
354 [ apply (le_minus_m a (a \mod b))
357 | rewrite < (times_n_Sm b c).
360 rewrite > (div_mod a b) in \vdash (? % ?)
361 [ rewrite > (sym_plus b ((a/b)*b)).
370 theorem lt_to_le_times_to_lt_S_to_div: ∀a,c,b:nat.
371 O < b → (b*c) ≤ a → a < (b*(S c)) → a/b = c.
372 #a #c #b #posb#lea #lta
373 @(div_mod_spec_to_eq … (a-b*c) (div_mod_spec_div_mod … posb …))
374 @div_mod_spec_intro [@lt_plus_to_minus // |/2/]
377 theorem div_times_times: ∀a,b,c:nat. O < c → O < b →
380 >(commutative_times b) <eq_div_div_div_times //
384 theorem times_mod: ∀a,b,c:nat.
385 O < c → O < b → (a*c) \mod (b*c) = c*(a\mod b).
387 @(div_mod_spec_to_eq2 (a*c) (b*c) (a/b) ((a*c) \mod (b*c)) (a/b) (c*(a \mod b)))
388 [>(div_times_times … posc) // @div_mod_spec_div_mod /2/
390 [applyS (monotonic_lt_times_r … c posc) /2/
391 |(applyS (eq_f …(λx.x*c))) //
396 theorem le_div_times_m: ∀a,i,m. O < i → O < m →
397 (a * (m / i)) / m ≤ a / i.
399 @(transitive_le ? ((a*m/i)/m))
401 |>eq_div_div_div_div // >div_times //
406 theorem le_div_times_Sm: ∀a,i,m. O < i → O < m →
407 a / i ≤ (a * S (m / i))/m.
409 apply (trans_le ? ((a * S (m/i))/((S (m/i))*i)))
410 [rewrite < (eq_div_div_div_times ? i)
411 [rewrite > lt_O_to_div_times
418 |apply le_times_to_le_div
420 |apply (trans_le ? (m*(a*S (m/i))/(S (m/i)*i)))
421 [apply le_times_div_div_times.
422 rewrite > (times_n_O O).
427 |rewrite > sym_times.
428 apply le_times_to_le_div2
429 [rewrite > (times_n_O O).