How to Generate Pascal's Triangle (Basic Method)

There are easier and more efficient ways to create Pascal's Triangle  based on the fabulous properties of the numbers in the triangle. But, today we are going to build a triangle from the basic definition. This may only work for a small number of rows, n (12?), but it helps understand some basic properties of the triangle. And you do not have to remember much else to create it. Let us see how this is done.
  • We first set the desired number of rows and allocate the order of the 2 dimensional array, which we will be using to display the results. Our rows and columns star from 0 to n.
  • We assign the value of nCr to each element in the array. The right half of the array is not going to be needed, since we want a triangle, not a square. So, we set the remaining elements equal to zero.
  • We define a function to calculate each nCr value, n!/[r!*(n-r)!]. This can be made a lot easier using other methods, but as per definition, we calculate each of the three factorials and use them in this formula. Lengthy process! 
Note: It is very important to define 0!=1 in the program whatever method you use to calculate nCr. You do not want to deal with errors such as those resulting from division by zero. There are some algorithms that can calculate elements of Pascal's Triangle without factorials! We will deal with them later.
Now for the FORTRAN program, 

 !To display Pascal triangle of small 'order'
program Pascal_triangle
    implicit none
    integer n,i,j
    integer, allocatable :: C(:,:)
  
    !To read n, the number of rows
    write (*,*) "How many rows of Pascal's Triangle do you want?"
    read (*,*) n
    write (*,*) "Displaying Pascal's triangle for n= ",n
    write(*,*) !Just a blank line for better presentation

    !To assign values of nCr to elements
    allocate (C(0:n,0:n))
    do i=0,n
          do j=0,n
               if (j.le.i) then
                C(i,j)=Comb(i,j)
                else
                C(i,j)=0
            end if
           end do
       end do

    !To display results
    do i=0,n
           write (*,*) (C(i,j),j=0,n)
    end do
 
    !To calculate nCr a really primitive way. Easy to remember  though
    contains
    integer function Comb (n,r)
    integer n,r,i, NFact, Rfact,N_RFact
  
        if (n==0) then !To calculate n!
            NFact=1
            else
            NFact=n
            do i=n-1,2,-1
                Nfact=Nfact*(i)
            end do
        end if
  
        if (r==0) then !To calculate r!
            RFact=1
            else
            RFact=r
            do i=r-1,2,-1
                Rfact=Rfact*(i)
            end do
           end if
  
        if ((n-r)==0) then !To calculate (n-r)!
            N_RFact=1
            else
            N_RFact=n-r
            do i=n-r-1,2,-1
                N_Rfact=N_Rfact*(i)
                end do
        end if
  
        Comb=Nfact/(RFact*N_RFact) !To calculate nCr by formula
    end function
  
end program Pascal_triangle

Sample Output: The First Five Rows of Pascal's Triangle
Each element of the triangle holds the value nCr, for the nth row and the rth column, where n and r both begin with 0. So, 4C0 is 1 and 5C3 is 10.

As we see in the program above,  we need to calculate the factorial 3 times for each nCr value. To make things easier we can split this function to calculate nCr into two separate functions, one for calculating the factorial and the other for calculating nCr. Let us look at what changes can be made in the program.


Now for the FORTRAN program,

!To display Pascal triangle of small order
program Pascal_triangle
    implicit none
    integer n,i,j
    integer, allocatable :: C(:,:)
   
     !To read n, the number of rows
    write (*,*) "How many rows of Pascal's Triangle do you want?"
    read (*,*) n
    write (*,*) "Displaying Pascal's triangle for n= ",n
    write(*,*)

    !To assign values of nCr to elements
    allocate (C(0:n,0:n))
    do i=0,n
          do j=0,n
               if (j.le.i) then
                C(i,j)=nCr(i,j)
                else
                C(i,j)=0
            end if
           end do
       end do

    !To display results
    do i=0,n
           write (*,*) (C(i,j),j=0,n)
    end do
 
    contains
   
        !To calculate Factorials Required
        integer function Fact(m)
            integer m,i !Declaring i is important to avoid altering do loops in the main function. You can use any other variable of your choice
   
            if (m==0) then !To calculate m!
                Fact=1
                else
                Fact=m
                do i=m-1,2,-1
                    Fact=Fact*(i)
                end do
            end if
       
        end function

        !To calculate nCr using factorials
        integer function nCr(n,r)
            integer  n,r
            nCr=Fact(n)/(Fact(r)*Fact(n-r))
        end function nCr
   
end program Pascal_triangle

This becomes a slightly shorter program since we use a function(fact) and calculate the value of another function (nCr) using this function. The output remains unnaffected.

You can use any number of functions in a Fortran program. They work just like intrinsic functions with arguments.

Notice that you write contains only once. And not every time you define a function.

No comments:

Post a Comment