def dist_carree(A,B):
return (B[0] - A[0])**2 + (B[1] - A[1])**2
A = (1, 4)
B = (-2, 6)
print(dist_carree(A,B))
def milieu(A,B):
return (B[0] + A[0])/2 , (B[1] + A[1])/2
A = (1, 4)
B = (-2, 6)
print(milieu(A,B))
Triplet Pythagoricien
def triplet_pythagoricien(n):
triplet = ()
for a in range(1, n + 1):
for b in range(a + 1, n + 1):
for c in range(b + 1, n + 1):
if a**2 + b**2 == c**2 :
triplet = triplet + ((a,b,c),)
return triplet
triplet_pythagoricien(100)
Autres Méthodes
# Version 1
def triplet_pythagoricien(n):
return tuple((a,b,c) for a in range(1, n + 1) for b in range(a + 1, n + 1) for c in range(b + 1, n + 1)if a**2 + b**2 == c**2)
triplet_pythagoricien(100)
# Version 2 - utilisant "yield"
def triplet_pythagoricien(n):
for a in range(1, n + 1):
for b in range(a + 1, n + 1):
for c in range(b + 1, n + 1):
if a**2 + b**2 == c**2 :
yield(a,b,c)
tuple(triplet_pythagoricien(100))
# Version 3
def triplet_pythagoricien(n):
return tuple((a,b,c) for a in range(1, n + 1) for b in range(a + 1, n + 1) for c in range(b + 1, n + 1)if a**2 + b**2 == c**2)
triplet_pythagoricien(100)
# Version 4 Utlisant une fonction "lambda"
triplet_pythagoricien = lambda n: tuple((a,b,c) for a in range(1, n + 1) for b in range(a + 1, n + 1) for c in range(b + 1, n + 1)if a**2 + b**2 == c**2)
triplet_pythagoricien(100)