Friday 19 October 2012

Pascal Triangle Through Python Recursive Program


Math has always been an interesting subject. Different interesting facts and theorems have made this subject even more interesting. One of them is the contribution of pascal in the form o f the what world call today "The Pascal Triangle". This triangle consists of rows of numbers with each row linked and completely dependent on the previous row.

 

In a row each number (except the 1's on the end points of the rows) is the sum of the two numbers that are above that number in the previous row.

Recursive Program: 

                                 Recursive program is a program that calls itself. This sort of program are capable of  solving problems in a lesser lines of code as compared to a simple program. Now here is a code that is written in python that calculates the pascal triangle for n number of rows and uses a recursive approach for doing this:

 

def triangle(n):
    temp=[]
   
    if n==0:
        return []
   
    if n==1:
        return [[1]]
   
    else:
        Pascal_triangle=[[1]]
            
        for i in range(1,n):
            print i
            for j in range(0,len(Pascal_triangle[i-1])):           
                print j
               
                if j==0:
                    temp.append(Pascal_triangle[i-1][0])
                    print temp
           
                else:
                    temp.append(Pascal_triangle[i-1][j-1]+Pascal_triangle[i-1][j])
                    print temp
            temp.append(1)
            Pascal_triangle.append(temp)
            temp=[]
   
    return Pascal_triangle      
                       

                          



7 comments:

  1. Ні there superb website! Doеѕ
    running a blοg similar to this take a lot of wοrk?
    I have virtuallу no κnowledge of сodіng howeνer I had been hоping tο ѕtart mу own blog ѕoοn.
    Anyhow, should you have anу iԁeаѕ
    or tеchniques foг new blog οωners pleаse shаrе.
    I understand thiѕ is off subjeсt neveгthelеsѕ Ι juѕt ωanteԁ to аsk.

    Thanks!

    My ωeb ѕіtе: roofers norman

    ReplyDelete
  2. I'm extremely impressed along with your writing skills and also with the structure for your weblog. Is that this a paid topic or did you customize it yourself? Either way stay up the excellent high quality writing, it's rare to see a nice blog like this
    one these days..

    Look at my web-site; Bankruptcy Attorney Florida

    ReplyDelete
  3. If you want to get a great deal from this post then you have to apply these techniques
    to your won weblog.
    See this: zobacz, http://abhorred.
    katowice.pl

    ReplyDelete
  4. I simply couldn't leave your website before suggesting that I extremely loved the usual info an individual supply in your guests? Is going to be back ceaselessly in order to investigate cross-check new posts

    Feel free to surf to my page; http://www.appliedbusinessforecasting.com/courses/user/view.php?id=16746&course=1

    ReplyDelete
  5. can any one write this code for emu8086 program plz ?????

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. def recursive_pascal(row, col):
    if (col == 0 or col == row):
    return 1
    else:
    return recursive_pascal(row - 1, col - 1) + recursive_pascal(row - 1, col)


    #this commenting stream doesn't allow for proper python spacing

    ReplyDelete