Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created August 7, 2019 22:15
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/8136186c69b32dc01dcaf2b61df4ebc1 to your computer and use it in GitHub Desktop.
Save parzibyte/8136186c69b32dc01dcaf2b61df4ebc1 to your computer and use it in GitHub Desktop.
private static boolean esNarcisista(int numero){
String numeroComoCadena = Integer.toString(numero);
int longitudDeNumero = numeroComoCadena.length();
double suma = 0;
for(int indice = 0; indice < longitudDeNumero; indice++){
// Convertir carácter a entero
int cifra = Character.getNumericValue(numeroComoCadena.charAt(indice));
/*
Elevarlo al exponente dado por la longitud del número
*/
double elevado = Math.pow(cifra, longitudDeNumero);
// Vamos sumando el resultado
suma = suma + 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