Méthode math.gcd() de Python
Python math.gcd()La méthode renvoie le plus grand commun diviseur des arguments entiers donnés.
gcd(0,0) renvoie 0.
Version de Python : 3.5
Modifié dans la version 3.9 : la prise en charge d'un nombre arbitraire d'arguments a été ajoutée ; les versions précédentes ne prenaient en charge que deux arguments.
Syntaxe
La syntaxe de la méthode math.gcd() est la suivante :
math.gcd(*integers)
Description du paramètre :
- *integers-- Obligatoire. Un nombre. Si x n'est pas un nombre, renvoie TypeError.
Valeur de retour
Renvoie un entier int représentant le plus grand commun diviseur (PGCD) de deux entiers ou plus.
Exemple
L'exemple suivant renvoie le plus grand commun diviseur de nombres :
Exemple
# Importer le package math
import math
# Afficher le plus grand commun diviseur
print (math.gcd(3, 6))
print (math.gcd(6, 12))
print (math.gcd(12, 36))
print (math.gcd(-12, -36))
print (math.gcd(5, 12))
print (math.gcd(10, 0))
print (math.gcd(0, 34))
print (math.gcd(0, 0))
import math
# Afficher le plus grand commun diviseur
print (math.gcd(3, 6))
print (math.gcd(6, 12))
print (math.gcd(12, 36))
print (math.gcd(-12, -36))
print (math.gcd(5, 12))
print (math.gcd(10, 0))
print (math.gcd(0, 34))
print (math.gcd(0, 0))
Résultat de sortie :
3 6 12 12 1 10 34 0Autres extensions
Module math de Python