Created
August 7, 2019 22:01
-
-
Save parzibyte/201c39dff71ad9831f2add4f13ae51c7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Determinar si un número es narcisista usando Python | |
parzibyte.me/blog | |
""" | |
def es_narcisista(numero): | |
numero_como_cadena = str(numero) | |
longitud_de_numero = len(numero_como_cadena) | |
suma = 0 | |
for letra in numero_como_cadena: | |
# Convertir carácter a entero | |
cifra_actual = int(letra) | |
# Elevar ese carácter a la potencia dada por la longitud del número | |
elevado = pow(cifra_actual, longitud_de_numero) | |
# El resultado lo añadimos a suma | |
suma = suma + elevado | |
# Comprobar si la suma al elevar es igual al número que recibimos | |
if numero == suma: | |
return True | |
else: | |
return False | |
# Hora de probar | |
numeros = [1, 2, 3, 4, 153, 28, 11, 96, 407, 1634, 54748] | |
for numero in numeros: | |
print("¿{} es narcisista? {}".format(numero, es_narcisista(numero))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment