Advertisement
apexsquirt

collatz weirdness

Feb 23rd, 2018
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.37 KB | None | 0 0
  1. <?php
  2.  
  3. $upto = 2000000;
  4. // change the value of THIS specific variable to graph up to some value (originally 2000000).
  5. // also, don't try any lower than 26000. there wouldn't be even a single point.
  6.  
  7. function collatz($x) {
  8.     if ($x % 2 == 0) {
  9.         return $x/2;
  10.     } else {
  11.         return 3*$x+1;
  12.     }
  13. }
  14.  
  15. $t = 0;
  16. $x = 0;
  17. $f = "";
  18.  
  19. for ($i = 1; $i <= $upto; $i++) { // $i is basically the variable x the plot is based upon.
  20.     $j = $i;
  21.     while ($j > 1) {
  22.         $j = collatz($j);
  23.         if ($j == 5) {
  24.             $t += 1; // t at $i = x is N(x).
  25.         }
  26.     }
  27.     $x += $t/$i; // the ratio N(x)/x.
  28.     print "($i) " . ($x/$i) . "\n"; // you can remove this line, this only displays every single value of N(x)/x.
  29.     if ($i % 1000 == 0) { // this would take too long to graph otherwise, but you can remove it if you're an absolute madman.
  30.         $f .= "<div style='position: absolute; left: " . ($i/1000) . "px; bottom: " .
  31.        
  32.         ($x/$i*((10-900)/(0.93701840707864 - 0.93766121527656))+(10-0.93701840707864*((10-900)/(0.93701840707864 - 0.93766121527656))))
  33.         // zooms such that the local minimum of the curve falls at 10px from the bottom and the maximum at 900px from it.
  34.         // you can, of course, replace 900 by something else if your screen resolution is different.
  35.        
  36.         . "px'>.</div>\n";
  37.     }
  38. }
  39. file_put_contents("full.html", $f); // the graph will be exported into "full.html" in the same folder as where this file is.
  40.  
  41. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement