fork download
  1. <?php
  2. function ulam_diag($N)
  3. {
  4. $result = array();
  5.  
  6. if ($N % 2 == 0) {
  7. for ($n = 0; $n < $N / 2; $n++) {
  8. $result[] = 4*$n*$n + 2*$n + 1;
  9. $result[] = 4*$n*$n + 4*$n + 2;
  10. $result[] = 4*$n*$n + 8*$n + 4;
  11. $result[] = 4*$n*$n + 6*$n + 3;
  12. }
  13. } else {
  14. $result[] = 1;
  15. for ($n = 1; $n <= ($N - 1) / 2; $n++) {
  16. $result[] = 4*$n*$n + 2*$n + 1;
  17. $result[] = 4*$n*$n + 4*$n + 1;
  18. $result[] = 4*$n*$n + 1;
  19. $result[] = 4*$n*$n - 2*$n + 1;
  20. }
  21. }
  22.  
  23. sort($result);
  24. return $result;
  25. }
  26.  
  27. print_r(ulam_diag(4));
  28. print_r(ulam_diag(3));
  29. ?>
Success #stdin #stdout 0.02s 52472KB
stdin
Standard input is empty
stdout
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 7
    [5] => 10
    [6] => 13
    [7] => 16
)
Array
(
    [0] => 1
    [1] => 3
    [2] => 5
    [3] => 7
    [4] => 9
)