1 | #!/usr/bin/python |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | # |
---|
4 | # Pyromaths |
---|
5 | # Un programme en Python qui permet de créer des fiches d'exercices types de |
---|
6 | # mathématiques niveau collège ainsi que leur corrigé en LaTeX. |
---|
7 | # Copyright (C) 2006 -- Jérôme Ortais (jerome.ortais@pyromaths.org) |
---|
8 | # |
---|
9 | # This program is free software; you can redistribute it and/or modify |
---|
10 | # it under the terms of the GNU General Public License as published by |
---|
11 | # the Free Software Foundation; either version 2 of the License, or |
---|
12 | # (at your option) any later version. |
---|
13 | # |
---|
14 | # This program is distributed in the hope that it will be useful, |
---|
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | # GNU General Public License for more details. |
---|
18 | # |
---|
19 | # You should have received a copy of the GNU General Public License |
---|
20 | # along with this program; if not, write to the Free Software |
---|
21 | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
22 | # |
---|
23 | |
---|
24 | from pyromaths.outils import Arithmetique |
---|
25 | import random |
---|
26 | from pyromaths.outils.Affichage import tex_coef |
---|
27 | |
---|
28 | # carres=[2,3,5,6,7,10,11,13,15,17,19] |
---|
29 | |
---|
30 | carres = [2, 3, 5, 6, 7, 10] # ,11,13,15,17,19] |
---|
31 | |
---|
32 | |
---|
33 | def exoaRb0(exo, cor, v): |
---|
34 | a = (tex_coef(v[0], '\\sqrt{%s}' % (v[6] * v[3] ** 2)), tex_coef(v[1], |
---|
35 | '\\sqrt{%s}' % (v[6] * v[4] ** 2), bplus=1), tex_coef(v[2], |
---|
36 | '\\sqrt{%s}' % (v[6] * v[5] ** 2), bplus=1)) |
---|
37 | exo.append(u'\\[ \\thenocalcul = ' + '%s%s%s' % a + '\\] ') |
---|
38 | cor.append(u'\\[ \\thenocalcul = ' + '%s%s%s' % a + '\\] ') |
---|
39 | a = (tex_coef(v[0], '\\sqrt{%s}' % v[3] ** 2), v[6], tex_coef(v[1], |
---|
40 | '\\sqrt{%s}' % v[4] ** 2, bplus=1), v[6], tex_coef(v[2], |
---|
41 | '\\sqrt{%s}' % v[5] ** 2, bplus=1), v[6]) |
---|
42 | cor.append(u'\\[ \\thenocalcul = ' + |
---|
43 | '%s\\times\\sqrt{%s}%s\\times\\sqrt{%s}%s\\times\\sqrt{%s}' % |
---|
44 | a + '\\] ') |
---|
45 | a = ( |
---|
46 | tex_coef(v[0], ''), |
---|
47 | v[3], |
---|
48 | v[6], |
---|
49 | tex_coef(v[1], '', bplus=1), |
---|
50 | v[4], |
---|
51 | v[6], |
---|
52 | tex_coef(v[2], '', bplus=1), |
---|
53 | v[5], |
---|
54 | v[6], |
---|
55 | ) |
---|
56 | cor.append(u'\\[ \\thenocalcul = ' + |
---|
57 | '%s\\times%s\\times\\sqrt{%s}%s\\times%s\\times\\sqrt{%s}%s\\times%s\\times\\sqrt{%s}' % |
---|
58 | a + '\\] ') |
---|
59 | a = (tex_coef(v[0] * v[3], '\\sqrt{%s}' % v[6]), tex_coef(v[1] * v[4], |
---|
60 | '\\sqrt{%s}' % v[6], bplus=1), tex_coef(v[2] * v[5], |
---|
61 | '\\sqrt{%s}' % v[6], bplus=1)) |
---|
62 | cor.append(u'\\[ \\thenocalcul = ' + '%s%s%s' % a + '\\] ') |
---|
63 | del a |
---|
64 | cor.append(u'\\[ \\boxed{\\thenocalcul = ' + '%s' % tex_coef(v[0] * v[3] + v[1] * v[4] + v[2] * |
---|
65 | v[5], '\\sqrt{%s}' % v[6]) + '} \\] ') |
---|
66 | |
---|
67 | |
---|
68 | def valeurs_aRb0(pyromax): # renvoie (coef0, coef1, coef2, carre0, carre1, carre2, b) |
---|
69 | a = carres |
---|
70 | l = [Arithmetique.valeur_alea(-pyromax, pyromax) for dummy in range(3)] |
---|
71 | while True: |
---|
72 | t = [random.randrange(2, pyromax) for dummy in range(3)] |
---|
73 | if pyromax < 4 or t[0] != t[1] and t[0] != t[2] and t[1] != t[2]: |
---|
74 | break |
---|
75 | l.extend(t) |
---|
76 | l.append(a[random.randrange(len(a))]) |
---|
77 | return tuple(l) |
---|
78 | |
---|
79 | |
---|
80 | def exoaRb1(exo, cor, v): |
---|
81 | a = (v[3] * v[0] ** 2, v[3] * v[1] ** 2, v[3] * v[2] ** 2) |
---|
82 | exo.append(u'\\[ \\thenocalcul = ' + '\\sqrt{%s}\\times\\sqrt{%s}\\times\\sqrt{%s}' % |
---|
83 | a + '\\] ') |
---|
84 | cor.append(u'\\[ \\thenocalcul = ' + '\\sqrt{%s}\\times\\sqrt{%s}\\times\\sqrt{%s}' % |
---|
85 | a + '\\] ') |
---|
86 | a = (v[0] ** 2, v[3], v[1] ** 2, v[3], v[2] ** 2, v[3]) |
---|
87 | cor.append(u'\\[ \\thenocalcul = ' + |
---|
88 | '\\sqrt{%s}\\times\\sqrt{%s}\\times\\sqrt{%s}\\times\\sqrt{%s}\\times\\sqrt{%s}\\times\\sqrt{%s}' % |
---|
89 | a + '\\] ') |
---|
90 | a = (v[0], v[3], v[1], v[3], v[2], v[3]) |
---|
91 | cor.append(u'\\[ \\thenocalcul = ' + |
---|
92 | '%s\\times\\sqrt{%s}\\times%s\\times\\sqrt{%s}\\times%s\\times\\sqrt{%s}' % |
---|
93 | a + '\\] ') |
---|
94 | a = ((v[0] * v[1]) * v[2], v[3], v[3]) |
---|
95 | cor.append(u'\\[ \\thenocalcul = ' + |
---|
96 | '%s\\times\\left(\\sqrt{%s}\\right)^2\\times\\sqrt{%s}' % |
---|
97 | a + '\\] ') |
---|
98 | a = ((v[0] * v[1]) * v[2], v[3], v[3]) |
---|
99 | cor.append(u'\\[ \\thenocalcul = ' + '%s\\times%s\\times\\sqrt{%s}' % a + '\\] ') |
---|
100 | del a |
---|
101 | cor.append(u'\\[ \\boxed{\\thenocalcul = ' + '%s' % tex_coef(((v[0] * v[1]) * v[2]) * v[3], |
---|
102 | '\\sqrt{%s}' % v[3]) + '} \\] ') |
---|
103 | |
---|
104 | |
---|
105 | def valeurs_aRb1(pyromax): # renvoie (coef0, coef1, coef2, carre0, carre1, carre2, b) |
---|
106 | a = carres |
---|
107 | while True: |
---|
108 | l = [random.randrange(2, pyromax) for dummy in range(3)] |
---|
109 | if pyromax < 4 or l[0] != l[1] and l[0] != l[2] and l[1] != l[2]: |
---|
110 | break |
---|
111 | l.append(a[random.randrange(len(a))]) |
---|
112 | return tuple(l) |
---|
113 | |
---|
114 | |
---|
115 | def valeurs_aPbRc(pyromax): # renvoie (coef0, coef1, coef2, carre0, carre1, carre2, b) |
---|
116 | while True: |
---|
117 | a = carres[random.randrange(len(carres))] |
---|
118 | b = carres[random.randrange(len(carres))] |
---|
119 | if a != b and Arithmetique.pgcd(a, b) != min(a, b): |
---|
120 | break |
---|
121 | while True: |
---|
122 | c = random.randrange(2, pyromax) |
---|
123 | d = Arithmetique.valeur_alea(-pyromax, pyromax) |
---|
124 | if c != d: |
---|
125 | break |
---|
126 | return (c, a, d, b) |
---|
127 | |
---|
128 | |
---|
129 | def exo_aPbRc(exo, cor, v): |
---|
130 | a = (tex_coef(v[0], '\\sqrt{%s}' % v[1]), tex_coef(v[2], |
---|
131 | '\\sqrt{%s}' % v[3], bplus=1)) |
---|
132 | exo.append(u'\\[ \\thenocalcul = ' + '\\left( %s%s \\right)^2' % a + '\\] ') |
---|
133 | cor.append(u'\\[ \\thenocalcul = ' + '\\left( %s%s \\right)^2' % a + '\\] ') |
---|
134 | if v[2] > 0: |
---|
135 | sgn = '+' |
---|
136 | else: |
---|
137 | sgn = '-' |
---|
138 | a = (tex_coef(v[0], '\\sqrt{%s}' % v[1], bpc=1), sgn, tex_coef(v[0], |
---|
139 | '\\sqrt{%s}' % v[1]), tex_coef(abs(v[2]), '\\sqrt{%s}' % v[3]), |
---|
140 | tex_coef(abs(v[2]), '\\sqrt{%s}' % v[3], bpc=1)) |
---|
141 | cor.append(u'\\[ \\thenocalcul = ' + '%s^2%s2\\times%s\\times%s+%s^2' % a + '\\] ') |
---|
142 | a = (v[0] ** 2, v[1], tex_coef((2 * v[0]) * v[2], '\\sqrt{%s}' % (v[1] * |
---|
143 | v[3]), bplus=1), v[2] ** 2, v[3]) |
---|
144 | cor.append(u'\\[ \\thenocalcul = ' + '%s\\times %s %s+%s\\times %s' % a + '\\] ') |
---|
145 | a = (v[0] ** 2 * v[1] + v[2] ** 2 * v[3], tex_coef((2 * v[0]) * v[2], |
---|
146 | '\\sqrt{%s}' % (v[1] * v[3]), bplus=1)) |
---|
147 | cor.append(u'\\[ \\boxed{\\thenocalcul = ' + '%s%s' % a + '} \\] ') |
---|
148 | |
---|
149 | |
---|
150 | def valeurs_entier0(pyromax): # renvoie (coef0, coef1, coef2, carre0, carre1, carre2, b) |
---|
151 | a = carres[random.randrange(len(carres))] |
---|
152 | while True: |
---|
153 | b = random.randrange(2, pyromax) |
---|
154 | c = Arithmetique.valeur_alea(-pyromax, pyromax) |
---|
155 | if b != c and abs(c) != 1: |
---|
156 | break |
---|
157 | return (b, c, a) |
---|
158 | |
---|
159 | |
---|
160 | def exo_entier0(exo, cor, v): |
---|
161 | a = (v[0], tex_coef(v[1], '\\sqrt{%s}' % v[2], bplus=1), v[0], |
---|
162 | tex_coef(-v[1], '\\sqrt{%s}' % v[2], bplus=1)) |
---|
163 | exo.append(u'\\[ \\thenocalcul = ' + '\\left( %s%s \\right)\\left( %s%s \\right)' % |
---|
164 | a + '\\] ') |
---|
165 | cor.append(u'\\[ \\thenocalcul = ' + '\\left( %s%s \\right)\\left( %s%s \\right)' % |
---|
166 | a + '\\] ') |
---|
167 | a = (tex_coef(v[0], '', bpc=1), tex_coef(abs(v[1]), '\\sqrt{%s}' % v[2], |
---|
168 | bpc=1)) |
---|
169 | cor.append(u'\\[ \\thenocalcul = ' + '%s^2-%s^2' % a + '\\] ') |
---|
170 | a = (v[0] ** 2, v[1] ** 2, v[2]) |
---|
171 | cor.append(u'\\[ \\thenocalcul = ' + '%s-%s\\times %s' % a + '\\] ') |
---|
172 | cor.append(u'\\[ \\boxed{\\thenocalcul = ' + '%s' % (v[0] ** 2 - v[1] ** 2 * v[2]) + '} \\] ') |
---|
173 | |
---|
174 | |
---|
175 | def valeurs_entier1(pyromax): # renvoie (coef0, coef1, coef2, carre0, carre1, carre2, b) |
---|
176 | a = carres[random.randrange(len(carres))] |
---|
177 | while True: |
---|
178 | v0 = random.randrange(2, pyromax) |
---|
179 | v1 = random.randrange(2, pyromax) |
---|
180 | b0 = random.randrange(2, pyromax) |
---|
181 | b1 = random.randrange(2, pyromax) |
---|
182 | a0 = (b1 * v0) * v1 |
---|
183 | a1 = v1 * b0 |
---|
184 | if b0 < b1 and Arithmetique.pgcd(a0, a1) != a1: |
---|
185 | break |
---|
186 | return (a0, b0, a1, b1, a) |
---|
187 | |
---|
188 | |
---|
189 | def exo_entier1(exo, cor, v): |
---|
190 | a = (tex_coef(v[0], '\\sqrt{%s}' % (v[1] ** 2 * v[4])), tex_coef(v[2], |
---|
191 | '\\sqrt{%s}' % (v[3] ** 2 * v[4]))) |
---|
192 | exo.append(u'\\[ \\thenocalcul = ' + '\\frac{%s}{%s}' % a + '\\] ') |
---|
193 | cor.append(u'\\[ \\thenocalcul = ' + '\\frac{%s}{%s}' % a + '\\] ') |
---|
194 | a = (v[0], v[1] ** 2, v[4], v[2], v[3] ** 2, v[4]) |
---|
195 | cor.append(u'\\[ \\thenocalcul = ' + |
---|
196 | '\\frac{%s\\times\\sqrt{%s}\\times\\cancel{\\sqrt{%s}}}{%s\\times\\sqrt{%s}\\times\\cancel{\\sqrt{%s}}}' % |
---|
197 | a + '\\] ') |
---|
198 | cor.append(u'\\[ \\thenocalcul = ' + '\\frac{%s\\times %s}{%s\\times %s}' % v[0:4] + '\\] ') |
---|
199 | cor.append(u'\\[ \\boxed{\\thenocalcul = ' + '%s' % (((v[0] * v[1]) // v[2]) // v[3]) + '} \\] ') |
---|
200 | |
---|
201 | def tex_racines(): |
---|
202 | exo = ['\\exercice'] |
---|
203 | exo.append('\\begin{enumerate}') |
---|
204 | exo.append(_(u'\\item Calculer les expressions suivantes et donner le résultat sous la forme $a\\,\\sqrt{b}$ avec $a$ et $b$ entiers, $b$ le plus petit possible.')) |
---|
205 | exo.append('\\begin{multicols}{2}\\noindent') |
---|
206 | cor = ['\\exercice*'] |
---|
207 | cor.append('\\begin{enumerate}') |
---|
208 | cor.append(_(u'\\item Calculer les expressions suivantes et donner le résultat sous la forme $a\\,\\sqrt{b}$ avec $a$ et $b$ entiers, $b$ le plus petit possible.')) |
---|
209 | mymax = 5 |
---|
210 | cor.append('\\begin{multicols}{2}\\noindent') |
---|
211 | valeurs = valeurs_aRb0(mymax) |
---|
212 | exoaRb0(exo, cor, valeurs) |
---|
213 | exo.append('\\columnbreak\\stepcounter{nocalcul}%') |
---|
214 | cor.append('\\columnbreak\\stepcounter{nocalcul}%') |
---|
215 | valeurs = valeurs_aRb1(mymax) |
---|
216 | exoaRb1(exo, cor, valeurs) |
---|
217 | exo.append('\\end{multicols}\\vspace{-3ex}') |
---|
218 | cor.append('\\end{multicols}') |
---|
219 | exo.append(_(u'\\item Calculer les expressions suivantes et donner le résultat sous la forme $a+b\\,\\sqrt{c}$ avec $a$, $b$ et $c$ entiers.')) |
---|
220 | exo.append('''\\stepcounter{nocalcul}% |
---|
221 | \\begin{multicols}{2}\\noindent''') |
---|
222 | cor.append(_(u'\\item Calculer les expressions suivantes et donner le résultat sous la forme $a+b\\,\\sqrt{c}$ avec $a$, $b$ et $c$ entiers.')) |
---|
223 | cor.append('''\\stepcounter{nocalcul}% |
---|
224 | \\begin{multicols}{2}\\noindent''') |
---|
225 | valeurs = valeurs_aPbRc(mymax) |
---|
226 | exo_aPbRc(exo, cor, valeurs) |
---|
227 | exo.append('\\columnbreak\\stepcounter{nocalcul}%') |
---|
228 | cor.append('\\columnbreak\\stepcounter{nocalcul}%') |
---|
229 | valeurs = valeurs_aPbRc(mymax) |
---|
230 | exo_aPbRc(exo, cor, valeurs) |
---|
231 | exo.append('\\end{multicols}\\vspace{-3ex}') |
---|
232 | cor.append('\\end{multicols}') |
---|
233 | exo.append(_(u"\\item Calculer les expressions suivantes et donner le résultat sous la forme d'un nombre entier.\n")) |
---|
234 | exo.append('''\\stepcounter{nocalcul}% |
---|
235 | \\begin{multicols}{2}\\noindent''') |
---|
236 | cor.append(_(u"\\item Calculer les expressions suivantes et donner le résultat sous la forme d'un nombre entier.\n")) |
---|
237 | cor.append('''\\stepcounter{nocalcul}% |
---|
238 | \\begin{multicols}{2}\\noindent''') |
---|
239 | valeurs = valeurs_entier0(mymax) |
---|
240 | exo_entier0(exo, cor, valeurs) |
---|
241 | exo.append('\\columnbreak\\stepcounter{nocalcul}%') |
---|
242 | cor.append('\\columnbreak\\stepcounter{nocalcul}%') |
---|
243 | valeurs = valeurs_entier1(mymax) |
---|
244 | exo_entier1(exo, cor, valeurs) |
---|
245 | exo.append('\\end{multicols}\\vspace{-3ex}') |
---|
246 | exo.append('\\end{enumerate}') |
---|
247 | cor.append('\\end{multicols}') |
---|
248 | cor.append('\\end{enumerate}') |
---|
249 | return (exo, cor) |
---|
250 | |
---|
251 | tex_racines.description = _(u'Racines carrées') |
---|