Advertisement
apexsquirt

[PHP-CLI] Ulam spiral

May 3rd, 2018
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.04 KB | None | 0 0
  1. <?php
  2. function is_prime($n) {
  3.     $s = 0;
  4.     for ($i = 1; $i <= floor(sqrt($n))+1; $i++) {
  5.         $s += floor($n/$i)-floor(($n-1)/$i);
  6.     }
  7.     if ($s == 1) { return true; }
  8.     else { return false; }
  9. }
  10. $height = 768;
  11. $width  = 1366;
  12. $top    = $height/4;
  13. $left   = $width/4;
  14. $move   = 0;
  15. $trace  = 1;
  16. $traceB = 1;
  17. $count  = 0;
  18. $next   = 0;
  19. //0 : move right
  20. //1 : move top
  21. //2 : move left
  22. //3 : move bottom
  23. $str = "";
  24. for ($i = 2; $i <= 100000; $i++) {
  25.     if ($count == $trace && $next == 0) {
  26.         $count = 0; $next = 1; $move++; $move = $move % 4;
  27.     } elseif ($count == $trace && $next == 1) {
  28.         $count = 0; $next = 0; $trace++; $move++; $move = $move % 4;
  29.     }
  30.     if ($count < $trace) {
  31.         $count++;
  32.     }
  33.     if ($move == 0) { $left++; }
  34.     elseif ($move == 1) { $top++; }
  35.     elseif ($move == 2) { $left--; }
  36.     elseif ($move == 3) { $top--; }
  37.     if (is_prime($i)) { $str .= "<div style='position: absolute; left: ".(2*$left)."px; bottom: ".(2*$top)."px'>.".
  38.         "</div>"; }
  39.     $str.="</div>";
  40.     if ($i % 1000 == 0) { print "$i\n"; }
  41. }
  42. file_put_contents(".html",$str);
  43. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement