Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created August 7, 2019 22:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/df5999dd6490638d63697826feaa0345 to your computer and use it in GitHub Desktop.
Save parzibyte/df5999dd6490638d63697826feaa0345 to your computer and use it in GitHub Desktop.
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