apexsquirt

Collatz conjecture (command line PHP windows)

Nov 1st, 2017
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.40 KB | None | 0 0
  1. <?php
  2. $input = readline("Up to... ? ");
  3. $s = "";
  4. for ($i = 1; $i <= $input; $i++) {
  5.     $a[0] = $i;
  6.     $s .= $a[0] . " ";
  7.     for ($j = 1; $j > 0; $j++) {
  8.         $a[$j] = ($a[$j-1] % 2)*(3*$a[$j-1] + 1) + (1 - ($a[$j-1] % 2))*($a[$j-1] / 2);
  9.         $s .= $a[$j] . " ";
  10.         if ($a[$j-3] . $a[$j-2] . $a[$j-1] == "421") { $j = -1; }
  11.     }
  12.     $s .= "\n";
  13.     print $i . " done\n";
  14. }
  15. file_put_contents("collatz.log", $s);
  16. ?>
Advertisement
Add Comment
Please, Sign In to add comment