Tuesday, April 16, 2013

Print Pascal's Triangle in PHP

Pascal's triangle is named after French mathematician Blaise Pascal. More info on this triangle can be found here. In this triangle, from 3rd row onwards, each number in the triangle is the sum of the two numbers appearing immediately above it. A small triangle is shown below.

                    1
                  1   1
                1   2   1
              1   3   3   1
            1   4   6   4   1
         1   5   10   10   5   1
       1   6   15   20   15   6   1
     1   7   21   35   35   21   7   1



How we try to solve this problem?

a. For the first row, we don't put a logic.
b. For 2nd row, we create an array of two ones. Then we print it.
c. For 3rd row onwards, we calculate sum of 2 digits appearing in the previous row.
   For 3rd row :: we create an array, set the first and last elements in the array to 1. Then we sum taking each 2 elements in the previous row i.e the array available in 2nd row. We start doing this sum from element index 1.
   For 4th row :: we create an array, set the first and last elements in the array to 1. Then we sum taking each 2 elements in the previous row i.e the array available in 3rd row.
   .... and so on till the total number of lines we are asked to print.
  
Check the code below which is quite self-explanatory.

<?php
//// Define Total Lines of the Pyramid
$total_lines = 10;

//// Define a Base Array
$base_arr = array(1,1);

//// PRINT LINE 1
pri_nt(1);

//// PRINT LINE 2
pri_nt(2, $base_arr);

//// Main LOOP to print from Line 3 onwards
for($i=3;$i<=$total_lines-2;$i++)
{
  //// CREATE ARRAY
  $arr = range(1,$i);
 
  //// Set 1st & Last item set to 1
  $arr[0] = $arr[count($arr)-1] = 1; 
 
  //// Get Previous Row's Array values for summation
  //// We start with index 1, zeroth element always holds 1
  for( $k=1; $k<count($arr)-1; $k++)
  {
    $arr[$k] = $base_arr[$k] + $base_arr[$k-1];
  }
 
  //// PRINT the new ARRAY
  pri_nt( $i, $arr );
 
  //// Preserve Current Array
  $base_arr = $arr;
}

function pri_nt( $cur_line, $arr = "" )
{
  global $total_lines;
 
  //// Printing Logic for 2nd Line Onwards
  if( is_array( $arr ) && !empty($arr) )
  {
     //// PRINT SPACES
     for( $i=1; $i<($total_lines-$cur_line)*2; $i++)
       echo "&nbsp;";
   
     //// PRINT DIGITS
     for( $i=0; $i<count($arr); $i++)
       echo "
&nbsp;&nbsp;&nbsp;" . $arr[$i] . "";
    
     //// New Line
     echo "<br>";
  }
  else   //// PRINT the ONE star at LINE 1
  {
    
     //// PRINT Spaces
     for( $i=1;$i<($total_lines-$cur_line)*2; $i++)
       echo "
&nbsp;";
   
     //// PRINT DIGIT 1
     echo "
&nbsp;&nbsp;&nbsp;"."1";
   
     //// New Line
     echo "<br>";   
  }

}
?>

3 comments:

Unknown said...

Hi there, I also have a problem like this one, but it's a little bit different. I need to have this output after an INPUT of any letter.

Enter a letter: E

so this must be the output if letter E is entered.

________________A
_______________ABA
______________ABCBA
_____________ABCDCBA
____________ABCDEDCBA

if letter F is entered it must be like this and so on.

________________A
_______________ABA
______________ABCBA
_____________ABCDCBA
____________ABCDEDCBA
___________ABCDEFEDCBA

P.S
(_)underscore are spaces. i just need to put it that way to type it faster. LOL

Unknown said...

Hi there, I also have a problem like this one, but it's a little bit different. I need to have this output after an INPUT of any letter.

Enter a letter: E

so this must be the output if letter E is entered.

________________A
_______________ABA
______________ABCBA
_____________ABCDCBA
____________ABCDEDCBA

if letter F is entered it must be like this and so on.

________________A
_______________ABA
______________ABCBA
_____________ABCDCBA
____________ABCDEDCBA
___________ABCDEFEDCBA

P.S
(_)underscore are spaces. i just need to put it that way to type it faster. LOL

Chandan Patra said...

Hi Yan,

Sorry, I am so late in replying, Did you find a solution to your problems? Or you want me to give you a solution?

Thanks