[Challenge] - Pascal's Triangle
Moderator: General Moderators
[Challenge] - Pascal's Triangle
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!
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
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> </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
Wow, well done. I hadn't even finished figuring out how I was going to do it on paper.
Re: [Challenge] - Pascal's Triangle
On paper it's easytimWebUK wrote:Wow, well done. I hadn't even finished figuring out how I was going to do it on paper.
1
121
..
Re: [Challenge] - Pascal's Triangle
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
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 :
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
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
Quick&dirty
45deg. rotated.
PS: error_reporting full off 
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>";There are 10 types of people in this world, those who understand binary and those who don't
Re: [Challenge] - Pascal's Triangle
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);
?>