Advertisement
apexsquirt

[PHP-CLI] A few Collatz-related stuff

Sep 25th, 2018
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.62 KB | None | 0 0
  1. <?php
  2. $counter = 1;
  3. ini_set("precision",13);
  4. $maxsteps = 0;
  5. $str = file_get_contents(".log");
  6. got_back:
  7. $array = [3,    //a in a/2^k x + b/2^k >= 0
  8.           1,    //k's max value
  9.           1,    //b lol
  10.           1];   //x's min value
  11. print "Randomized test #$counter\n";
  12. $str .= "#$counter : ";
  13. for ($i = 1; $i > 0; $i++) {
  14.     $temp = [3*$array[0]/(2**$array[1]),
  15.              min(rand(1,intval(log(3*$array[0])/log(2))),intval(log(3*$array[0])/log(2))),
  16.              3*$array[2]/(2**$array[1]),
  17.              ceil(max($array[3],(3*$array[2]+1)/(2**(intval(log(3*$array[0])/log(2))+1)-3*$array[0])))];
  18.     $temp[3] = ceil($temp[3]);
  19.     print "Step $i : (".round($temp[0],5)."a_0+".round($temp[2],5).")/2^k>=a_0 iff k<=".round(intval(log(3*$array[0])/log(2)),5)." when a_0>=".round($temp[3],5).". ";
  20.     $fullcounter++;
  21.     print "We'll choose k=".round($temp[1],3);
  22.     $minmax = max($minmax,$temp[3]);
  23.     $avgxmin += $temp[3];
  24.     $maxk = max($maxk,round(intval(log(3*$array[0])/log(2)),5));
  25.     $avgkmax += round(intval(log(3*$array[0])/log(2)),5);
  26.     $array = $temp;
  27.     if ($temp[1] <= 0) {
  28.         print "...\nOh wait, we had k>0. Contradiction !\n\n";
  29.         $maxsteps = max($maxsteps,$i);
  30.         $avgsteps += $i;
  31.         print "(Average nĀ° of steps : ".($avgsteps/$counter).", average minimal value of a_0 required in a step : ".($avgxmin/$counter).", average max k : ".($avgkmax/$counter).")\n\n\n\n";
  32.         $str .= "Average nĀ° of steps : ".($avgsteps/$counter).", average minimal value of a_0 required in a step : ".($avgxmin/$fullcounter).", average max k : ".($avgkmax/$fullcounter)."\n";
  33.         if ($counter % 100 == 0) { file_put_contents(".log",$str); }
  34.         $counter++;
  35.         goto got_back;
  36.     }
  37.     print "\n";
  38. }
  39. goto got_back;
  40. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement