[Challenge] - Pascal's Triangle

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
timWebUK
Forum Contributor
Posts: 239
Joined: Thu Oct 29, 2009 6:48 am
Location: UK

[Challenge] - Pascal's Triangle

Post 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!
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: [Challenge] - Pascal's Triangle

Post 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>");
?>
User avatar
timWebUK
Forum Contributor
Posts: 239
Joined: Thu Oct 29, 2009 6:48 am
Location: UK

Re: [Challenge] - Pascal's Triangle

Post by timWebUK »

Wow, well done. I hadn't even finished figuring out how I was going to do it on paper.
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: [Challenge] - Pascal's Triangle

Post 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
..

;)
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: [Challenge] - Pascal's Triangle

Post 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;
User avatar
timWebUK
Forum Contributor
Posts: 239
Joined: Thu Oct 29, 2009 6:48 am
Location: UK

Re: [Challenge] - Pascal's Triangle

Post 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 :
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: [Challenge] - Pascal's Triangle

Post 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.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: [Challenge] - Pascal's Triangle

Post 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 ;)
There are 10 types of people in this world, those who understand binary and those who don't
dalx
Forum Newbie
Posts: 1
Joined: Wed May 19, 2010 6:15 pm

Re: [Challenge] - Pascal's Triangle

Post 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);

?>
Post Reply