def __mul__(self, other): """Multiplie un objet SquareRoot par un nombre. >>> from pyromaths.classes.SquareRoot import SquareRoot >>> SquareRoot([3,45],3)*SquareRoot([2,45],-1) SquareRoot([['6*45', None], [-3, 45], [6, 45], [-3, None]]) """ if not isinstance(other, SquareRoot): other = SquareRoot([other, None]) reduction = False if self.EstReductible(): self = self.simplifie() reduction = True if other.EstReductible(): other = other.simplifie() reduction = True if reduction: return '%r*%r' % (self, other) lprod = [] for e in self.racines: for f in other.racines: if e[1] == None or f[1] == None: lprod.append([e[0] * f[0], max(e[1], f[1])]) elif e[1] == f[1]: lprod.append(['%r*%r' % (e[0] * f[0], e[1]), None]) elif carrerise(e[1]) == 1 or carrerise(f[1]) == 1: if carrerise(e[1]) == 1: e[0], e[1] = e[0] * int(sqrt(e[1])), 1 if carrerise(f[1]) == 1: f[0], f[1] = f[0] * int(sqrt(f[1])), 1 lprod.append([e[0] * f[0], e[1] * f[1]]) else: lprod.append([e[0] * f[0], e[1] * f[1]]) return SquareRoot(lprod)
def Decompose(self): """ Décompose une unique racine carrée de la forme a*sqrt(b^2*c) en a*sqrt(b^2)*sqrt(c) >>> from pyromaths.classes.SquareRoot import SquareRoot >>> SquareRoot([5, 8]).Decompose() 'SquareRoot([[5, 4]])*SquareRoot([[1, 2]])' :rtype: string """ racine = self.racines[0] if racine[1] == None: return repr(racine[0]) if isinstance(racine[1], int): complement = carrerise(racine[1]) if complement == 1: if racine[0] == 1: return int(sqrt(racine[1])) if racine[0] == -1: return -int(sqrt(racine[1])) if racine[1] == 1: return str(racine[0]) return '%r*%r' % (racine[0], int(sqrt(racine[1]))) if complement == racine[1]: return repr(self) return '%r*%r' % (SquareRoot([racine[0], racine[1] / complement ]), SquareRoot([1, complement])) raise ValueError(u'Not Implemented : SquareRoot(%s)' % racine)
def __init__(self): pol = [creerPolydegre2(nb_racines=2, rac_radical=False, rac_quotient=False)] pol.append(creerPolydegre2(nb_racines=1)) a, b = valeur_alea(-9, 9), valeur_alea(-9, 9) pol.append([[a ** 2, 2], [-b ** 2, 0]]) a, b = valeur_alea(-9, 9), valeur_alea(-9, 9) pol.append([[a, 2], [b, 1]]) a, b = valeur_alea(-9, 9), valeur_alea(-9, 9) pol.append([[a, 2], [b, 0]]) pol.pop(randrange(1, len(pol))) pol.pop(randrange(1, len(pol))) pol.pop(randrange(1, len(pol))) shuffle(pol) exercice = [pol] lval = [[[randrange(2, 10) * (-1) ** randrange(2), 1], [randrange(2, 10) * (-1) ** randrange(2), 0]] for dummy in range(3)] a, b, c = -lval[2][0][0] * lval[1][0][0], lval[0][0][0] - lval[2][0][0] * lval[1][1][0] - lval[2][1][0] * lval[1][0][0], lval[0][1][0] - lval[2][1][0] * lval[1][1][0] delta = b ** 2 - 4 * a * c while delta < 0 or carrerise(delta) != 1: lval = [[[randrange(2, 10) * (-1) ** randrange(2), 1], [randrange(2, 10) * (-1) ** randrange(2), 0]] for dummy in range(3)] a, b, c = -lval[2][0][0] * lval[1][0][0], lval[0][0][0] - lval[2][0][0] * lval[1][1][0] - lval[2][1][0] * lval[1][0][0], lval[0][1][0] - lval[2][1][0] * lval[1][1][0] delta = b ** 2 - 4 * a * c # print delta, Polynome([[a, 2], [b, 1], [c, 0]]), '\cfrac{%s}{%s}' % (-b - sqrt(delta), 2 * a), '\cfrac{%s}{%s}' % (-b + sqrt(delta), 2 * a) exercice.append(lval) shuffle(exercice) self.exercice = exercice
def Decompose(self): """ Décompose une unique racine carrée de la forme a*sqrt(b^2*c) en a*sqrt(b^2)*sqrt(c) >>> from pyromaths.classes.SquareRoot import SquareRoot >>> SquareRoot([5, 8]).Decompose() 'SquareRoot([[5, 4]])*SquareRoot([[1, 2]])' :rtype: string """ racine = self.racines[0] if racine[1] == None: return repr(racine[0]) if isinstance(racine[1], int): complement = carrerise(racine[1]) if complement == 1: if racine[0] == 1: return int(sqrt(racine[1])) if racine[0] == -1: return -int(sqrt(racine[1])) if racine[1] == 1: return str(racine[0]) return '%r*%r' % (racine[0], int(sqrt(racine[1]))) if complement == racine[1]: return repr(self) return '%r*%r' % (SquareRoot([racine[0], racine[1] / complement]), SquareRoot([1, complement])) raise ValueError(u'Not Implemented : SquareRoot(%s)' % racine)
def simplifie_racine(n): '''renvoie coeff,radicande où sqrt(n)=coeff*sqrt(radicande)''' if n == 0: return 0, 0 else: ncar = carrerise(n) return int(sqrt(n // ncar)), ncar
def EstDecomposable(self): """ Renvoie True si une des racines est de la forme sqrt{a**2*b} avec a != 1 >>> from pyromaths.classes.SquareRoot import SquareRoot >>> SquareRoot([5, 8], [1, 7]).EstDecomposable() True >>> SquareRoot([5, 7], [1, 7]).EstDecomposable() False :rtype: Boolean """ for e in self.racines: if e[1] != None and (carrerise(e[1]) != e[1] or e[1] == 1): return True return False
def __init__(self): [a, b, c, d] = [randrange(-5, 6) for dummy in range(4)] while a == 0 or c == 0 or a ** 2 * d - a * b * c + c ** 2 < 0 or carrerise(a ** 2 * d - a * b * c + c ** 2) != 1: [a, b, c, d] = [randrange(-5, 6) for dummy in range(4)] p1 = str(Polynome([[a, 1], [b, 0]], "m")) p2 = str(Polynome([[c, 1], [d, 0]], "m")) pol = [Polynome([[1, 2], [p1, 1], [p2, 0]]), randrange(3)] exercice = [list(pol)] v = [randrange(-4, 5) for dummy in range(6)] while v[0] == 0 or v[2] == v[4] == 0 or reduce(lambda x, y: x * y, v) != 0 or v[2] == v[3] == 0 or v[4] == v[5] == 0: v = [randrange(-4, 5) for dummy in range(6)] lp = [str(Polynome([[v[2 * i] / pgcd(v[2 * i], v[2 * i + 1]), 1], [v[2 * i + 1] / pgcd(v[2 * i], v[2 * i + 1]), 0]], "a")) for i in range(3)] pol = Polynome([[lp[0], 2], [lp[1], 1], [lp[2], 0]]) vi = Fraction(-v[1], v[0]) racine = randrange(-4, 5) while racine == vi or racine == 0: racine = randrange(-4, 5) if vi.d == 1 : vi = str(vi.n) else: vi = str(vi) exercice.append([list(pol), vi, racine]) self.exercice = exercice
def Arithmetique(): """Exercice de décomposition de nombres en facteurs premiers, puis de recherche du PGCD et du PPCM, et d'applications aux fractions""" # ## Question 1 exo = ["\\exercice", '\\begin{enumerate}', u"\\item Donner la décomposition" + u" en facteurs premiers des nombres suivants, et préciser quand il" + u" s\'agit d\'un nombre premier :\\par"] cor = ["\\exercice*", '\\begin{enumerate}', u"\\item Donner la décomposition" + u" en facteurs premiers des nombres suivants, et préciser quand il" + u" s\'agit d\'un nombre premier :\\par"] prime = premiers[randint(10, 167)] fauxpgcd = randint(1, 101) fauxpgcdfactor = factoriseTex(fauxpgcd)[0] complementaires = [randint(6, 50), randint(6, 50)] pgcdcompl = pgcd(complementaires[0], complementaires[1]) pgcdcomplfact = factoriseTex(pgcdcompl)[0] if pgcdcompl != 1: facteurs = pgcdcomplfact + fauxpgcdfactor facteurs.sort() else: facteurs = fauxpgcdfactor autresnombres = [randint(51, 999), randint(51, 999)] listenombres = [prime, complementaires[0] * fauxpgcd, complementaires[1] * fauxpgcd, ] + autresnombres melange = [prime, complementaires[0] * fauxpgcd, complementaires[1] * fauxpgcd, ] + autresnombres shuffle(melange) exo.append(decimaux(melange[0]) + " ; " + decimaux(melange[1]) + " ; " + decimaux(melange[2]) + " ; " + decimaux(melange[3]) + " ; " + decimaux(melange[4]) + " ;") cor.append("\\begin{multicols}{2}") for i in range(5): cor += factoriseTex(melange[i])[1] cor.append("\\end{multicols}") # ## Question 2 exo.append(u'\\item En déduire le PGCD et le PPCM des nombres ' + decimaux(listenombres[1]) + " et " + decimaux(listenombres[2]) + ".") cor.append(u'\\item En déduire le PGCD et le PPCM des nombres ' + decimaux(listenombres[1]) + " et " + decimaux(listenombres[2]) + ".\\par") cor.append(u"D'après la question 1), on sait que les nombres " + decimaux(listenombres[1]) + " et " + decimaux(listenombres[2]) + " ont comme facteurs premiers communs : ") for j in range(len(facteurs)): if j == 0: temp = "$" if j != len(facteurs) - 1: temp += decimaux(facteurs[j]) + " , " else: temp += decimaux(facteurs[j]) + "$.\\par" cor.append(temp) cor.append(u"On en déduit que le PGCD des nombres " + decimaux(listenombres[1]) + " et " + decimaux(listenombres[2]) + " est : ") temp = "$" if len(facteurs) > 1: for j in range(len(facteurs)): if j != len(facteurs) - 1: temp += decimaux(facteurs[j]) + " \\times " else: temp += decimaux(facteurs[j]) + " = " temp += decimaux(fauxpgcd * pgcdcompl) + ".$\\par" cor.append(temp) vraippcm = (listenombres[1] * listenombres[2]) / (fauxpgcd * pgcdcompl) if (listenombres[1] % listenombres[2] == 0): cor.append(decimaux(listenombres[1]) + u" est un multiple de " + decimaux(listenombres[2]) + u", donc leur PPCM est directement " + decimaux(listenombres[1]) + ".") elif (listenombres[2] % listenombres[1] == 0): cor.append(decimaux(listenombres[2]) + u" est un multiple de " + decimaux(listenombres[1]) + u", donc leur PPCM est directement " + decimaux(listenombres[2]) + ".") else: cor.append(u"Il existe plusieurs méthodes pour calculer le PPCM de " + decimaux(listenombres[1]) + " et de " + decimaux(listenombres[2]) + ".\\par") cor.append(u"En voici deux :") cor.append("\\begin{enumerate}") cor.append(u"\\item On peut simplement utiliser la formule :") cor.append(u"$a \\times b = PGCD(a;~b) \\times PPCM(a;~b)$.\\par") cor.append(u"Donc : $PPCM(" + decimaux(listenombres[1]) + ";~" + decimaux(listenombres[2]) + ") = " + "\\dfrac{" + decimaux(listenombres[1]) + "\\times" + decimaux(listenombres[2]) + "}{" + decimaux(fauxpgcd * pgcdcompl) + "} = " + decimaux(vraippcm) + ".$") cor.append(u"\\item On peut aussi multiplier un nombre par les \"facteurs " + u"complémentaires\" de l'autre.\n" + u"Ces \"facteurs " + u"complémentaires\" sont les facteurs qui complètent le PGCD pour " + u"former le nombre.\\par") temp = u"Comme $PGCD(" + decimaux(listenombres[1]) + ";~" + \ decimaux(listenombres[2]) + ") = " + decimaux(fauxpgcd * pgcdcompl) if len(facteurs) > 1: temp += " = " for j in range(len(facteurs)): if j != len(facteurs) - 1: temp += decimaux(facteurs[j]) + " \\times " else: temp += decimaux(facteurs[j]) factornb1 = factoriseTex(listenombres[1])[0] if len(factornb1) > 1: textcompl = u"$, alors les \"facteurs complémentaires\" de $" else: textcompl = u"$, alors le \"facteur complémentaire\" de $" temp += textcompl + decimaux(listenombres[1]) + " = " for j in range(len(factornb1)): if j != len(factornb1) - 1: temp += decimaux(factornb1[j]) + " \\times " else: temp += decimaux(factornb1[j]) factcompl = factoriseTex(listenombres[1] / (fauxpgcd * pgcdcompl))[0] if len(factcompl) == 1: temp += u"$ est : " else: temp += u"$ sont : " for j in range(len(factcompl)): if j != len(factcompl) - 1: temp += decimaux(factcompl[j]) + " , " else: temp += decimaux(factcompl[j]) + ".\n" temp += u"On en déduit que $PPCM(" + decimaux(listenombres[1]) + ";~" + \ decimaux(listenombres[2]) + ") = " + decimaux(listenombres[2]) + \ " \\times " for j in range(len(factcompl)): if j != len(factcompl) - 1: temp += decimaux(factcompl[j]) + " \\times " else: temp += decimaux(factcompl[j]) + " = " temp += decimaux(vraippcm) + ".$" cor.append(temp) cor.append("\\end{enumerate}") # ## Question 3 exo.append(u"\\item Quel est le plus petit nombre par lequel il faut " + u"multiplier " + decimaux(autresnombres[0]) + u" pour obtenir un carré parfait ?") cor.append(u" \\item Pour obtenir un carré parfait, il faut que sa " + u"décomposition en facteurs premiers ne contienne que des facteurs " + u"apparaissant un nombre pair de fois. D'après la question 1, " + u"la décomposition en facteurs premiers de " + decimaux(autresnombres[0])) decompautre = factoriseTex(autresnombres[0])[1] if len(decompautre) == 1: cor.append(u" est lui-même, car c'est un nombre premier.") else: cor.append(" est : \\par\n$" + decimaux(autresnombres[0]) + " = " + decompautre[-2][5:-2] + ".$\\par") cor.append(u"Il faut donc encore multiplier ce nombre par ") carre = carrerise(autresnombres[0]) factsup = factoriseTex(carre)[0] if len(factsup) == 1: cor.append(" le facteur ") else: cor.append(" les facteurs ") for j in range(len(factsup)): if (j != len(factsup) - 1) and (j != len(factsup) - 2): cor.append(decimaux(factsup[j]) + " , ") elif (j == len(factsup) - 2): cor.append(decimaux(factsup[j]) + " et ") else: cor.append(decimaux(factsup[j]) + ".\\par") cor.append(u"Le nombre cherché est par conséquent " + decimaux(carre) + u" et le carré parfait obtenu est " + decimaux(carre * autresnombres[0]) + ".") # ## Question 4 exo.append(u"\\item Rendre la fraction $\\dfrac{" + decimaux(listenombres[1]) + "}{" + decimaux(listenombres[2]) + u"}$ irréductible.") cor.append(u"\\item Le moyen le plus rapide de simplifier cette fraction est" + u"de diviser le numérateur et le dénominateur par leur PGCD." + u" D'après la question 2), PGCD(" + decimaux(listenombres[1]) + ";~" + decimaux(listenombres[2]) + ") = " + decimaux(fauxpgcd * pgcdcompl) + ", donc on obtient :\\par") cor.append(u"$\dfrac{" + decimaux(listenombres[1]) + "{\\scriptstyle \\div " + decimaux(fauxpgcd * pgcdcompl) + "}}{" + decimaux(listenombres[2]) + "{\\scriptstyle \\div " + decimaux(fauxpgcd * pgcdcompl) + "}} = \dfrac{" + decimaux(listenombres[1] / (fauxpgcd * pgcdcompl)) + "}{" + decimaux(listenombres[2] / (fauxpgcd * pgcdcompl)) + "}.$") # ## Question 5 num = [randint(6, 50), randint(6, 50)] exo.append(u"\\item Calculer $\\dfrac{" + decimaux(num[0]) + "}{" + decimaux(listenombres[1]) + "} + \\dfrac{" + decimaux(num[1]) + "}{" + decimaux(listenombres[2]) + "}$.") mult1 = vraippcm / listenombres[1] mult2 = vraippcm / listenombres[2] num1 = mult1 * num[0] num2 = mult2 * num[1] simplfin = pgcd(num1 + num2, vraippcm) if simplfin != 1: simpl = "{\\scriptstyle \\div " + decimaux(simplfin) + "}" result = " = \\dfrac{" + decimaux((num1 + num2) / simplfin) + "}{" + \ decimaux((vraippcm) / simplfin) + "}" else: simpl = "" result = "" cor.append(u"\\item Il faut mettre les fractions au même dénominateur. Grâce" + u"à la question 2), nous avons déjà un dénominateur commun : " + u"le PPCM des nombres " + decimaux(listenombres[1]) + " et " + decimaux(listenombres[2]) + u", qui est par définition le plus petit" + u"multiple commun de ces deux nombres.\\par") cor.append(u"$\\dfrac{" + decimaux(num[0]) + "{\\scriptstyle \\times " + decimaux(mult1) + "}}{" + decimaux(listenombres[1]) + "{\\scriptstyle \\times " + decimaux(mult1) + "}} + \\dfrac{" + decimaux(num[1]) + "{\\scriptstyle \\times " + decimaux(mult2) + "}}{" + decimaux(listenombres[2]) + "{\\scriptstyle \\times " + decimaux(mult2) + "}} = \\dfrac{" + decimaux(num1) + "}{" + decimaux(vraippcm) + "} + \\dfrac{" + decimaux(num2) + "}{" + decimaux(vraippcm) + "} = \\dfrac{" + decimaux(num1 + num2) + simpl + "}{" + decimaux(vraippcm) + simpl + "}" + result + ".$") exo.append("\\end{enumerate}") cor.append("\\end{enumerate}") return (exo, cor)