Page 1 of 1

[Challenge] - Pascal's Triangle

Posted: Fri Feb 12, 2010 5:11 am
by timWebUK
Hi guys,

Just thought of this. Write a PHP script that can output Pascal's Triangle for as many rows as the user specifies.

FYI: http://en.wikipedia.org/wiki/Pascal's_triangle

Enjoy!

Re: [Challenge] - Pascal's Triangle

Posted: Fri Feb 12, 2010 5:27 am
by Apollo
To my own surprise it worked at my first try :)

Code: Select all

<?php
$n = $_POST['n'];
if (!$n) die("<form method='post'>How many? <input type='text' name='n'> <input type='submit'></form>");
print("<table>");
$e = "<td>&nbsp;</td>";
for ($i=0; $i<=$n; $i++)
{
    $row = array();
    $row[] = 1;
    $s = str_repeat($e,$n-$i);
    print("<tr>$s");
    for ($j=0; $j<$i; $j++)
    {
        if ($j)
        {
            $row[] = $prev[$j-1]+$prev[$j];
            print($e);
        }
        print("<td>$row[$j]</td>");
    }
    $row[] = 0;
    $prev = $row;
    print("$s</tr>");
}
print("</table>");
?>

Re: [Challenge] - Pascal's Triangle

Posted: Fri Feb 12, 2010 5:43 am
by timWebUK
Wow, well done. I hadn't even finished figuring out how I was going to do it on paper.

Re: [Challenge] - Pascal's Triangle

Posted: Fri Feb 12, 2010 5:48 am
by papa
timWebUK wrote:Wow, well done. I hadn't even finished figuring out how I was going to do it on paper.
On paper it's easy
1
121
..

;)

Re: [Challenge] - Pascal's Triangle

Posted: Fri Feb 12, 2010 8:19 am
by Eran
Similar to appolo's solution but in my opinion a little bit cleaner:

Code: Select all

<style>
    p {margin:5px; text-align:center;}
    b {margin:0 5px;}
</style>
<?php
$rows = 10;
$triangle = '';
$numbers = array(0 => array(0));
for($row = 1;$row <= $rows; $row++) {
    $numbers[$row] = array();
    $triangle .= '<p>';
    for($column = 1; $column <= $row; $column++) {
        $number = ($column == 1 || $column == $row ? 1 : ($numbers[$row - 1][$column - 1] + $numbers[$row - 1][$column]));
        $numbers[$row][$column] = $number;
        $triangle .= '<b>' . $number . '</b>';
    }
    $triangle .= '</p>';
}
echo $triangle;

Re: [Challenge] - Pascal's Triangle

Posted: Fri Feb 12, 2010 8:31 am
by timWebUK
Hi pytrin. Nice job.

I was wondering if you could explain the following lines of syntax:

$numbers = array(0 => array(0));

and

$numbers[$row] = array();

and I can't quite figure out this either

? 1 :

Re: [Challenge] - Pascal's Triangle

Posted: Fri Feb 12, 2010 8:56 am
by Eran
Just initializing the arrays. I usually follow this practice instead of casting the variables to array by adding members to it with the [] operator. It will work the same without those lines.

Re: [Challenge] - Pascal's Triangle

Posted: Sat Feb 20, 2010 4:13 am
by VladSun
Quick&dirty :) 45deg. rotated.

Code: Select all

function col($prev, $num)
{
    if (!--$num)
        return;
 
    echo "<tr>";
    for($i=0; $i<$num; $i++)
        echo "<td>".($prev[$i] = $prev[$i] + $prev[$i-1])."</td>";
    echo "</tr>";
 
    col($prev, $num);
}
 
echo "<table>";
col(array(1), 20);
echo "</table>";
PS: error_reporting full off ;)

Re: [Challenge] - Pascal's Triangle

Posted: Wed May 19, 2010 6:18 pm
by dalx
Just another recursive example of left aligned Pascal Triangle.

Code: Select all

<style>
.pt {float:left; min-width:50px; text-align:right;}
</style>

<?php
$n = $_POST['n'];
if (!$n) die("<form method='post'>How many? <input type='text' name='n'> <input type='submit'></form>");

function pt($prev, $level, $max_level)
{
  if ($level > $max_level) return;
  $row = array();
  $row[] = 1;
  for ($j=0; $j<$level; $j++)
  {
      if ($j) $row[] = $prev[$j-1]+$prev[$j];
      print("<div class=\"pt\">$row[$j]</div>");
  }
  print("<br>");
  unset($prev);
  pt($row, $level+1, $max_level);  
}

pt(array(), 0, $n);

?>