Created
August 7, 2019 22:16
-
-
Save parzibyte/df5999dd6490638d63697826feaa0345 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
func esNarcisista(numero int) bool{ | |
numeroComoCadena := strconv.Itoa(numero) | |
longitudDeNumero := len(numeroComoCadena) | |
suma := 0 | |
for indice := 0; indice < longitudDeNumero; indice++{ | |
// Convertir carácter a entero | |
cifraActual := numeroComoCadena[indice] - '0' | |
/* | |
Elevarlo al exponente dado por la longitud del número | |
por cierto, se castea a float64 porque math.Pow necesita un float64 | |
*/ | |
elevado := math.Pow(float64(cifraActual), float64(longitudDeNumero)) | |
// Vamos sumando el resultado | |
suma = suma + int(elevado) | |
} | |
// Finalmente comprobamos si el número es igual a la suma | |
if suma == numero{ | |
return true | |
}else{ | |
return false | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment