Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created August 7, 2019 22:12
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/b9fc13a447ead98f5b94a0bff37dba89 to your computer and use it in GitHub Desktop.
Save parzibyte/b9fc13a447ead98f5b94a0bff37dba89 to your computer and use it in GitHub Desktop.
<?php
function esNarcisista($numero){
$numeroComoCadena = strval($numero);
$longitudDelNumero = strlen($numeroComoCadena);
$suma = 0;
for($indice = 0; $indice < $longitudDelNumero; $indice++){
// Convertir carácter a entero
$caracterActual = $numeroComoCadena[$indice];
$cifraComoEntero = intval($caracterActual);
// Elevar ese carácter a la potencia dada por la longitud del número
$elevado = pow($cifraComoEntero, $longitudDelNumero);
// El resultado lo añadimos a $suma
$suma = $suma + $elevado;
}
// Comprobar si la suma al elevar es igual al número que recibimos
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