Created
August 7, 2019 22:00
-
-
Save parzibyte/7317cfe2532dd9559089b41adc7f9601 to your computer and use it in GitHub Desktop.
Número narcisista en Python - https://parzibyte.me/blog/2018/10/02/numero-narcisista-python/
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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment