Advertisement
apexsquirt

[PHP-CLI] Collatz related sequence

Jul 5th, 2018
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.03 KB | None | 0 0
  1. <?php
  2. ini_set("memory_limit",268435456);
  3. //ini_set("memory_limit","-1"); pour qu'ça affiche pas d'message d'erreur,
  4. //                              mais ça peut bouffer bcp d'mémoire,
  5. //                              donc c'est ssi tu sais c'que tu fais.
  6. //                              sinon, tu mets juste un truc supérieur à
  7. //                              268435456 pour avoir plus de termes.
  8. ${"a"."0"} = [16];
  9. for ($i = 1; $i > 0; $i++) {
  10.     for ($j = 0; $j < count(${"a".($i-1)}); $j++) {
  11.         $t = count(${"a".($i-1)}) - 1;
  12.         if ((${"a".($i-1)}[$j]/3-1/3) == floor(${"a".($i-1)}[$j]/3-1/3)) {
  13.             $t++;
  14.             ${"a".($i-1)}[$t] = ${"a".($i-1)}[$j]/3-1/3;
  15.         }
  16.     }
  17.     for ($j = 0; $j < count(${"a".($i-1)}); $j++) {
  18.         ${"a$i"}[$j] = 2*${"a".($i-1)}[$j];
  19.     }
  20.     sort(${"a".($i-1)});
  21.     $t = 0;
  22.     for ($j = 0; $j < count(${"a".($i-1)}); $j++) {
  23.         if (${"a".($i-1)}[$j]+1 == ${"a".($i-1)}[$j+1]) {
  24.             $t++;
  25.         }
  26.     }
  27.     if ($i % 15 > 1) { print ", $t"; }
  28.     elseif ($i % 15 == 0) { print ", $t,\n"; }
  29.     elseif ($i % 15 == 1) { print "$t"; }
  30. }
  31. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement