Created
April 13, 2014 14:21
-
-
Save AntoineAugusti/10586115 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
$nb_annees = 20; // NE MODIFIEZ QUE CE PARAMETRE PUIS CHARGEZ CETTE PAGE | |
// A partir de cet endroit, toute modification est à vos risques et périls ;D | |
$nb_date_palindrome = 0; | |
$nb_test_palindrome = 0; | |
$nb_tests_max = $nb_annees * 365; | |
function microtime_float() { | |
list($usec, $sec) = explode(" ", microtime()); | |
return ((float)$usec + (float)$sec); | |
} | |
function number_space ($number) { | |
$number_space = number_format($number, 0, ',', ' '); // Arrondi et espaces sur les milliers | |
return $number_space; | |
} | |
$time_start = microtime_float(); | |
function date_is_palindrome($date) { | |
return (preg_match("#[0-9]{8}$#", $date)) AND ($date{0} == $date{7} AND $date{1} == $date{6} AND $date{2} == $date{5} AND $date{3} == $date{4}); | |
} | |
function convert_to_date($date) { | |
$jour = substr($date, 0, 2); | |
$mois = substr($date, 2, 2); | |
$annee = substr($date, 4, 4); | |
$new_date = ''.$jour.'/'.$mois.'/'.$annee.''; | |
return $new_date; | |
} | |
// Boucle de test des dates | |
for ($i='1';$i <= $nb_tests_max;$i++) { | |
if ($i == '1') | |
$tomorrow_timestamp = mktime(0, 0, 0, date("m") , date("d"), date("Y")); // Initialise la date lors de la première itération | |
$date = date("dmY", $tomorrow_timestamp); | |
// Découpage de la date | |
$jour = substr($date, 0, 2); | |
$mois = substr($date, 2, 2); | |
$annee = substr($date, 4, 4); | |
$annee_begin = substr($date, 4, 2); | |
$annee_last = $date{7}; | |
if (strrev($annee_begin) <= '12' AND $annee_last <= '3' AND strrev($annee_begin) == $mois) { | |
$test_palindrome = date_is_palindrome($date); | |
$nb_test_palindrome++; | |
} | |
else | |
$test_palindrome = FALSE; | |
if ($test_palindrome) { | |
$date_palindrome = convert_to_date($date); | |
echo '<b>'.$date_palindrome.' est un palindrome !<br></b>'; | |
$nb_date_palindrome++; | |
} | |
// Retourne le lendemain, pour recommencer la boucle | |
$tomorrow_timestamp = mktime(0, 0, 0, $mois , $jour+1, $annee); | |
} | |
// Calculs pour les statistiques | |
$last_date = convert_to_date($date); | |
$pourcentage_palindrome = round($nb_date_palindrome / $nb_tests_max * 100, 4); | |
$pourcentage_test_palindrome = round($nb_test_palindrome / $nb_tests_max * 100, 4); | |
$time_end = microtime_float(); | |
$time = round($time_end - $time_start, 8); | |
$nombre_de_tests_par_seconde = number_space(round($nb_tests_max / $time)); | |
$nb_test_palindrome = number_space($nb_test_palindrome); | |
$nb_tests_max = number_space($nb_tests_max); | |
// Affichage des statistiques | |
echo '<br /><hr />'; | |
echo 'La dernière date à avoir été testée est le '.$last_date.'.<br>'; | |
echo 'Nombre de tests de palindromes effectué ('.$nb_test_palindrome.') : '.$pourcentage_test_palindrome.' % du nombre de dates total.<br>'; | |
echo ''.$nb_date_palindrome.' dates sont des palindromes dans les '.$nb_annees.' prochaines années, soit '.$pourcentage_palindrome.' % des dates.<br>'; | |
echo 'Page générée en '.$time.' s avec '.$nb_tests_max.' tests soit '.$nombre_de_tests_par_seconde.' tests par seconde.'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment