Created
August 7, 2019 22:12
-
-
Save parzibyte/b9fc13a447ead98f5b94a0bff37dba89 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
<?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