Puisqu'on a lu la deuxième question, on en profite pour tester la
fonction en décomposant le nombre 36 :
def triplet_produit(n) :
L = []
for a in range(1, int(n**(1/3))+1) :
for b in range(a, int(n**0.5)+1 ) :
c = n // (a*b)
if a*b*c == n and c >= b:
L.append([a,b,c])
return L
for t in triplet_produit(36):
print(t)
On tente d'afficher les triplets (a,b,c)
tels que
a
\(\leqslant\) b
\(\leqslant\) c
avec abc = n
.
Les bornes choisies s'expliquent par le raisonnement suivant :
- On a \( a^3 \leqslant n \) car si \(a^3 > n\) alors \( abc \geqslant a^3 > n \).
- On a \( b^2 \leqslant n \) car si \(b^2 > n\) alors \( abc \geqslant 1\times b^2 > n \).
On peut éviter la racine cubique avec les instructions suivantes :
def triplet_produit(n) :
L = []
a = 1
while a*a*a <= n :
for b in range(a, int(n**0.5)+1 ) :
c = n // (a*b)
if a*b*c == n and c >= b :
L.append([a,b,c])
a += 1
return L
for t in triplet_produit(36):
print(t)
On ajoute les sommes des triplets à la fin de la liste renvoyée :
def triplet_produit_plus(n) :
L = []
a = 1
while a*a*a <= n :
for b in range(a, int(n**0.5)+1 ) :
c = n // (a*b)
if a*b*c == n and c >= b :
L.append([a,b,c, a+b+c])
a += 1
return L
for t in triplet_produit_plus(36):
print(t)
On obtient 16 triplets suivis de leur somme :
[1, 1, 36, 38]
[1, 2, 18, 21]
[1, 3, 12, 16]
[1, 4, 9, 14]
[1, 6, 6, 13]
[2, 2, 9, 13]
[2, 3, 6, 11]
[3, 3, 4, 10]
Si le facteur a encore un doute avec la somme, c'est qu'il y a ambiguïté.
Le seul cas laissant un doute est le cas de la somme 13.
Comme il y a une aînée, on élimine le triplet 1, 6, 6 et les âges des
filles de Mme Gtroifi sont 2, 2, 9.
Prenez le temps de bien analyser la ligne 21 de notre proposition de solution.
def triplet_produit_plus(n) :
L = []
a = 1
while a*a*a <= n :
for b in range(a, int(n**0.5)+1 ) :
c = n // (a*b)
if a*b*c == n and c >= b :
L.append([a,b,c, a+b+c])
a += 1
return L
def liste_sommeNonUnique(n) :
""" renvoie la liste des triplets (a,b,c) de produit n en ne gardant que les
triplets donnant une somme obtenue avec un autre triplet."""
L_triplet = triplet_produit_plus(n)
L = []
for i in range(len(L_triplet)) :
if L_triplet[i][3] in [L_triplet[j][3] for j in range(len(L_triplet)) if j!=i] :
L.append(L_triplet[i])
return L
def cas_interessant(M) :
"""renvoie les valeurs de n <= M pouvant se décomposer en produits de trois entiers
de diverses façons avec une même somme."""
L = []
for k in range(2,M+1):
if len(liste_sommeNonUnique(k)) != 0 :
L.append(k)
return L
print(cas_interessant(200))
On obtient :
[36, 40, 72, 90, 96, 126, 144, 168, 176, 200]
def triplet_produit_plus(n) :
L = []
a = 1
while a*a*a <= n :
for b in range(a, int(n**0.5)+1 ) :
c = n // (a*b)
if a*b*c == n and c >= b :
L.append([a,b,c, a+b+c])
a += 1
return L
def liste_sommeNonUnique_etDoublon(n) :
""" renvoie la liste des triplets (a,b,c) de produit n en ne gardant que les
triplets donnant une somme obtenue avec un autre triplet."""
L_triplet = triplet_produit_plus(n)
L = []
for i in range(len(L_triplet)) :
if L_triplet[i][3] in [L_triplet[j][3] for j in range(len(L_triplet)) if j!=i] :
if L_triplet[i][0] == L_triplet[i][1] or L_triplet[i][1] == L_triplet[i][2] :
L.append(L_triplet[i])
return L
def cas_interessant(M) :
"""renvoie les valeurs de n <= M pouvant se décomposer en produits de trois entiers
de diverses façons avec une même somme."""
L = []
for k in range(2,M+1):
if len(liste_sommeNonUnique_etDoublon(k)) != 0 :
L.append(k)
return L
print(cas_interessant(1000))
On obtient :
[36, 40, 72, 90, 144, 225, 234, 252, 288,
297, 320, 360, 432, 450, 576, 648, 675, 720,
784, 800, 816, 850, 880, 900, 972]