Advertisement
mate2code

De Bruijn's triangle (oeis.org/A187783)

Oct 27th, 2014
828
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.13 KB | None | 0 0
  1. <?php
  2.  
  3. // Creating the b-file of the array https://oeis.org/A187783
  4. // This code uses the GMP library for big integers: http://php.net/manual/en/book.gmp.php
  5.  
  6. // functions:
  7.  
  8. function pascalDiag($m, $n) {
  9.     $return = gmp_div_q(
  10.         gmp_fact($m * $n),
  11.         gmp_pow(
  12.             gmp_fact($n),
  13.             $m
  14.         )
  15.     );
  16.     return gmp_strval($return);
  17. }
  18.  
  19. function diagCoordinates($n) {
  20.     $arr = array();
  21.     for ($i = 0; $i < $n; $i++) {
  22.         for ($j = 0; $j <= $i; $j++) {
  23.             array_push($arr, array($j, $i - $j));
  24.         }
  25.     }
  26.     return $arr;
  27. }
  28.  
  29. // create array:
  30.  
  31. $num = 55;  // For higher numbers, the entries in the b-file would have more than 1000 digits.
  32.  
  33. $arr = array();
  34.  
  35. for ($i=0; $i<=$num; $i++) {
  36.     $row = array();
  37.     for ($j=0; $j<=$num-$i; $j++) {
  38.         array_push($row, pascalDiag($i,$j));
  39.     }
  40.     array_push($arr, $row);
  41. }
  42.  
  43. // create b-file:
  44.  
  45. $coord = diagCoordinates($num-1);
  46. $coordLen = count($coord);
  47.  
  48. $str = '';
  49.  
  50. for ($i=0; $i<$coordLen; $i++) {
  51.     $val = $arr[ $coord[$i][0] ][ $coord[$i][1] ];
  52.     $str .= $i . ' ' . $val . "\n";
  53. }
  54.  
  55. echo $str;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement