Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Pascal's Triangle using recursion in php

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 988
    Comment on it

    Hello readers, today we will discuss printing pascal's triangle using recursion. Pascal's triangle is a triangle where each number is the sum of the two numbers directly above it. We have one of the most interesting Number Patterns in Pascal's Triangle. In this triangle, each row is the power of 11, we have 1 in first row which is 11 raise to the power 0, 11 in second row which is 11 raise to the power 1 and 121 in third row ,which is 11 raise to the power 2, 1331 in fourth row which is 11 raise to the power 3 and so on. It is also a triangular array of binomial coefficients.

    This triangle is infinite. because it has no bottom side and the horizontal sum of each row get doubles to the above row and also a sum of each row is the powers of 2.

     

    Here is the code:

    <?php
    
    
    $no_of_rows = 15;
    $array_store = array();
    
    
    function get_element($r,$p)   
    {
        global $array_store;
    
        if(isset($array_store[$r][$p]))
        {
            return $array_store[$r][$p];
        }
        if($r==1 and $p==1)
        {
            return 1;
        }
        else if($p==0)  
        {
            return 0;
        }
        else if($p>$r)  
        {
            return 0;
        }
        return get_element($r-1,$p-1)+get_element($r-1,$p);   
    }
    
    
    ?>


     

    <pre>
    
    
    <?php
    
    
    for($i=1;$i<=$no_of_rows;$i++)
    {
        for($j=1;$j<=$i;$j++)
        {
            echo $array_store[$i][$j]=get_element($i,$j);  
    
            if($i!=$j)
            {
                echo ' ';
            }
        }
        echo '<br>';
    }
    
    
    ?>
    
    
    </pre>

     

     

    Output:

     

    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
    1 8 28 56 70 56 28 8 1
    1 9 36 84 126 126 84 36 9 1
    1 10 45 120 210 252 210 120 45 10 1
    1 11 55 165 330 462 462 330 165 55 11 1
    1 12 66 220 495 792 924 792 495 220 66 12 1
    1 13 78 286 715 1287 1716 1716 1287 715 286 78 13 1
    1 14 91 364 1001 2002 3003 3432 3003 2002 1001 364 91 14 1

     

     

    Comments: Recursion is a method of solving problems by breaking it into smaller and smaller subproblems until you get smallest part which can be solved easily. Recursion function is the function which is calling itself again and again. In th above example I have made a function get_element(), having two arguments, this function is a recursive function, because it is calling itself until the condition satisfy.

     

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: