Advertisement
Guest User

Untitled

a guest
May 20th, 2015
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.62 KB | None | 0 0
  1. <?php
  2.  
  3. $num = gmp_init(2, 10);     // 2 in base 10
  4. $bases = range(6, 3, -1);   // Bases, descending order
  5.  
  6. $iteration = 0;
  7. $update_interval = 1000;
  8.  
  9. $skip_base = -1;
  10. $last_iter_time = microtime(true);
  11.  
  12. // Until we find an answer...
  13. while (true) {
  14.     $pass = true;
  15.  
  16.     foreach ($bases as $b) {
  17.         if ($b == $skip_base) { continue; }
  18.        
  19.         // Convert to base $b
  20.         $result = gmp_strval($num, $b);
  21.  
  22.         if ( next_candidate($result) ) {
  23.             $pass = false;
  24.             $num = gmp_init($result, $b);
  25.            
  26.             // The number is known to be binary in base $b, skip it next iteration
  27.             $skip_base = $b;
  28.             break;
  29.         }
  30.     }
  31.  
  32.     // Display progress every $update_interval attempts
  33.     $iteration++;
  34.     if ($iteration % $update_interval == 0) {
  35.         $iter_per_sec = $update_interval / (microtime(true) - $last_iter_time);
  36.        
  37.         printf("Iteration %s; Value ~10^%s; Iter/sec %.1f" . PHP_EOL,
  38.             number_format($iteration),
  39.             number_format( strlen(gmp_strval($num, 10)) - 1 ),
  40.             $iter_per_sec
  41.         );
  42.        
  43.         $last_iter_time = microtime(true);
  44.     }
  45.  
  46.     if ($pass) { break; }
  47. }
  48.  
  49.  
  50. $result = gmp_strval($num, 10);
  51. $result_len = strlen($result);
  52.  
  53. printf("Result found after %s iterations, result is %s digits long\n",
  54.     number_format($iteration),
  55.     number_format($result_len)
  56. );
  57.  
  58. if ($result_len <= 100) {
  59.     echo $result . "\n";
  60. }
  61.  
  62. file_put_contents("result.num", $result);
  63. exit(0);
  64.  
  65.  
  66.  
  67.  
  68.  
  69. // Returns TRUE if the number was changed, FALSE otherwise
  70. function next_candidate(&$num) {
  71.     $num_len = strlen($num);
  72.     $last_zero  = -1;
  73.     $reject_pos = -1;
  74.    
  75.     // Find the first non-binary character and its location or return if there isn't one
  76.     if ( preg_match('/[^01]/', $num, $hits, PREG_OFFSET_CAPTURE) ) {
  77.         $reject_pos = $hits[0][1];
  78.        
  79.     } else {
  80.         return false;
  81.     }
  82.    
  83.    
  84.     // Find the last zero before the reject character
  85.     $last_zero = strrpos( $num, "0", -1*($num_len - $reject_pos) );
  86.  
  87.    
  88.     if ($last_zero == false) {
  89.         // If there were no zeroes before the reject, the next candidate is a one followed by N zeros,
  90.         //   where N is the length of the original number
  91.         // e.g. "111" => "1000"
  92.         $num = "1" . str_repeat("0", $num_len);
  93.        
  94.     } else {
  95.         // If there was a zero before the reject, replace it with a 1 and everything after with a zero.
  96.         $num = substr($num, 0, $last_zero) . "1" . str_repeat("0", $num_len - $last_zero - 1);
  97.     }
  98.    
  99.     return true;
  100. }
  101.  
  102. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement