Created
February 9, 2016 04:01
-
-
Save helmerdavila/7302d487d900fface660 to your computer and use it in GitHub Desktop.
Prueba Orbis
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 | |
/** | |
* Averiguar ######, que es la sumatoria de los 12 numeros primos capicúas de | |
* 5 cifras que solo usen 2 digitos, uno par y el otro impar | |
* (ejemplos de estos numeros: 11411, 72727, 78887) | |
*/ | |
function es_primo($numero) | |
{ | |
$divisores = 0; | |
for ($auxNumero = 1; $auxNumero <= $numero; $auxNumero++) { | |
if ($numero % $auxNumero == 0) { | |
$divisores++; | |
if ($divisores > 2) { | |
break; | |
} | |
} | |
} | |
if ($divisores == 2) { | |
return true; | |
} | |
return false; | |
} | |
function es_capicua($numero) | |
{ | |
$stringNum = (string) $numero; | |
$inversa = strrev($stringNum); | |
if ($stringNum === $inversa) { | |
return true; | |
} | |
return false; | |
} | |
function es_par($cifra) | |
{ | |
if ($cifra % 2 == 0) { | |
return true; | |
} | |
return false; | |
} | |
function dos_cifras($numero) | |
{ | |
$cifraPar = false; | |
$cifraImpar = false; | |
$stringNum = (string) $numero; | |
$arrayNum = str_split($stringNum); | |
$arrayCifras = []; | |
foreach ($arrayNum as $cifra) { | |
if (es_par($cifra)) { | |
$cifraPar = true; | |
} else { | |
$cifraImpar = true; | |
} | |
$arrayCifras[intval($cifra)] = true; | |
if (count($arrayCifras) > 2) { | |
break; | |
} | |
} | |
if (count($arrayCifras) == 2 && $cifraPar == true && $cifraImpar == true) { | |
return true; | |
} | |
return false; | |
} | |
$arregloNum = []; | |
echo '<pre>'; | |
for ($numero = 11111; $numero <= 99999; $numero++) { | |
if (es_capicua($numero)) { | |
if (es_primo($numero)) { | |
if (dos_cifras($numero)) { | |
$arregloNum[] = $numero; | |
} | |
} | |
} | |
} | |
print_r($arregloNum); | |
print_r(array_sum($arregloNum)); | |
echo '</pre>'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment