| 1 | <?php\r |
| 2 | //Settings\r |
| 3 | $start = 0;\r |
| 4 | $stop = 1000;\r |
| 5 | $places = strlen($stop);\r |
| 6 | $tryes = 50; //Tryes to find better\r |
| 7 | $base = 10;\r |
| 8 | //Init\r |
| 9 | error_reporting(0);\r |
| 10 | srand(time());\r |
| 11 | \r |
| 12 | /////////////////////////////////////////////////////////////////\r |
| 13 | //\r |
| 14 | function rada_add($name, $istr) { //Funkce pro pridani do rady -> !!! OPTIMALIZOVAT !!! <-\r |
| 15 | if(!eregi($istr, $GLOBALS[$name])) {\r |
| 16 | if( $istr[0] == $GLOBALS[$name][strlen($GLOBALS[$name])-1] ) {\r |
| 17 | $istr = substr($istr, 1);\r |
| 18 | $GLOBALS[$name] = $GLOBALS[$name].$istr;\r |
| 19 | } elseif( $GLOBALS[$name][0] == $istr[strlen($istr)-1] ) { \r |
| 20 | $GLOBALS[$name] = substr($GLOBALS[$name], 1);\r |
| 21 | $GLOBALS[$name] = $istr.$GLOBALS[$name];\r |
| 22 | } else {\r |
| 23 | \r |
| 24 | //Nahodne na zacetek nebo konec\r |
| 25 | if(rand(0,1)) { \r |
| 26 | $GLOBALS[$name] = $istr.$GLOBALS[$name];\r |
| 27 | } else {\r |
| 28 | $GLOBALS[$name] = $GLOBALS[$name].$istr;\r |
| 29 | }\r |
| 30 | \r |
| 31 | }\r |
| 32 | }\r |
| 33 | //$GLOBALS[$name]=$GLOBALS[$name].$istr; //Debug\r |
| 34 | }\r |
| 35 | \r |
| 36 | //\r |
| 37 | function rada_unique($name) { //Funkce pro vyhledani a smazani duplikatu (i potrebnych)\r |
| 38 | $length = $GLOBALS["places"];\r |
| 39 | $replacer = "%";\r |
| 40 | \r |
| 41 | for($i=0;$i<(strlen($GLOBALS[$name])-$length);$i++) {\r |
| 42 | \r |
| 43 | $sub = substr($GLOBALS[$name], $i, $length);\r |
| 44 | for($x=0;$x<$length;$x++) {\r |
| 45 | $GLOBALS[$name][($i+$x)] = $replacer;\r |
| 46 | }\r |
| 47 | \r |
| 48 | $GLOBALS[$name] = str_replace($sub, "", $GLOBALS[$name]); //Smaze duplikaty\r |
| 49 | \r |
| 50 | $replace="";\r |
| 51 | for($x=0;$x<$length;$x++) {\r |
| 52 | $replace = $replace.$replacer;\r |
| 53 | }\r |
| 54 | \r |
| 55 | $GLOBALS[$name] = str_replace($replace, $sub, $GLOBALS[$name]);\r |
| 56 | }\r |
| 57 | \r |
| 58 | }\r |
| 59 | \r |
| 60 | //\r |
| 61 | function add_zeroes($num, $places) { //Zarovna cislo na $places mist pridanim nul na zacatek\r |
| 62 | $str = "";\r |
| 63 | for($i=0;$i<$places;$i++) {\r |
| 64 | $str = $str."0";\r |
| 65 | }\r |
| 66 | //$num = base_convert($num, 10, $base); //BASE CONVERT -> !!! NOT WORKS YET !!! <-\r |
| 67 | $str=$str.$num;\r |
| 68 | $str = substr($str, strlen($str)-$places);\r |
| 69 | return $str;\r |
| 70 | }\r |
| 71 | \r |
| 72 | ///CODE///////////////////////////////////////////\r |
| 73 | //Test\r |
| 74 | //die(add_zeroes(23, 5));\r |
| 75 | //////////////////////////////////////////////////\r |
| 76 | $try = $tryes;\r |
| 77 | $next = true;\r |
| 78 | while($next) { //MAIN LOOP\r |
| 79 | $next = false;\r |
| 80 | \r |
| 81 | $rada = ""; //Vytvorime prazdnou radu\r |
| 82 | for($i=$start;$i<=$stop;$i++) { //Pridame vsechna n-cisli\r |
| 83 | $istr = add_zeroes($i, $places);\r |
| 84 | rada_add("rada", $istr);\r |
| 85 | }\r |
| 86 | \r |
| 87 | rada_unique("rada");\r |
| 88 | \r |
| 89 | $notfound = 0;\r |
| 90 | for($i=$start;$i<=$stop;$i++) {\r |
| 91 | $istr = add_zeroes($i, $places);\r |
| 92 | if(!eregi($istr, $rada)) {\r |
| 93 | $notfound++;\r |
| 94 | //echo("Not found: $i\n");\r |
| 95 | $next = true;\r |
| 96 | }\r |
| 97 | }\r |
| 98 | \r |
| 99 | echo("Total not found: $notfound\n"); //Debug\r |
| 100 | \r |
| 101 | if($try > 0) {\r |
| 102 | if($notfound <= $minnotfound || !isset($minnotfound)) {\r |
| 103 | echo("-minnf: $notfound\n"); //Debug\r |
| 104 | $minnotfound = $notfound;\r |
| 105 | $try = $tryes;\r |
| 106 | } else {\r |
| 107 | $try--;\r |
| 108 | }\r |
| 109 | } else {\r |
| 110 | if($notfound <= $minnotfound || $notfound == 0) $next = false;\r |
| 111 | }\r |
| 112 | \r |
| 113 | } //MAIN LOOP\r |
| 114 | \r |
| 115 | for($i=$start;$i<=$stop;$i++) { //Pridame vsechna zbyla n-cisli\r |
| 116 | $istr = add_zeroes($i, $places);\r |
| 117 | rada_add("rada", $istr);\r |
| 118 | }\r |
| 119 | \r |
| 120 | /////////OUTPUT///////////////////////////////////\r |
| 121 | echo("\n");\r |
| 122 | echo("Length: ".strlen($rada)." chars\n");\r |
| 123 | echo("Total not found: $notfound\n");\r |
| 124 | echo($rada);\r |
| 125 | echo("\n\n");\r |