Algorithm for Pascal's Triangle. (Explained below) |
- As you can see in the picture, we have set all elements at the left and right edges of the triangle equal to 1.These correspond to nC0 and nCn.
- Since we are using a square array of size (0:n), we set all elements other than those required by the triangle equal to zero.
- We also initialize the remaining elements which we will be calculating to some initial value. I have used -1 to make it different from 1 and 0 used in the above steps.
- Note the three blue hexagons/blocks in the image above. The sum of the elements of the upper two gives the element in the bottom block.1+1=2, 1+_2=3, 1+3=4, 3+3=6, etc. This is true for all elements expect those on the left and right edges, which are set to 1.
- Note the pink blocks in the picture. You will see that the triangle is symmetric. Therefore, we do not need to calculate elements on the right as they are the same as those on the left. This comes from the fact that nCr=nC(n-r). Therefore, we simply calculate the elements of a particular row upto the middle column and then assign the values to remaining elements based on the elements corresponding to them. This has to be done before the elements of the next row are calculated. For example, in the figure, the value of 3 to the right has to be assigned before 3+3=6 is calculated in the next row.
- Finally, we display results and/or save them to an output file for future references.
Now for the Fortran program,
!To display Pascal's Triangle
program pascal_property
implicit none
integer i,j,n,middle
real m
integer, allocatable:: nCr(:,:)
write (*,*) "Give the value of n for the Pascal's Triangle"
read (*,*) n
write (*,*) "The value of n is= ",n
allocate (nCr(0:n,0:n))
!To initialize elements
do i =0,n
do j=0,n
if (j==0) then !To set all elements in the first column to 1 as nC0=1
nCr(i,j)=1
else if (i==j) then !To set all elements nCr where n=r to 1 as nCn=1
nCr(i,j)=1
else if (j.gt.i) then !To set elements not needed in the array to 0
nCr(i,j)=0
else
nCr(i,j)=-1 !To initialize the rest of the elements. Used -1 to differentiate from 1 and 0 used earlier.
end if
end do
end do
do i=0,n
m=i !It is important to give a real value for the ceiling function. Hence, we cannot use the variable 'i' directly. You can also use real (i) in the function instead
if (mod(m,2.0)==0) then !To calculate values for half of the triangle. 2.0 is used instead of 2. Arguments of modulus function must be of the same type (integer or real)
middle=ceiling(m/2)!To calculate the variable 'middle' based on simple logic
else
middle=floor(m/2)
end if
do j=0,i
if (nCr(i,j)==-1) then
if (j.le.middle) then
nCr(i,j)=nCr(i-1,j-1)+nCr(i-1,j)
nCr(i,i-j)=nCr(i,j) !To calculate elements in the remaining half using the property of symmetry, nCr=nC(n-r)
end if
end if
end do
end do
open (1,file='pasc.dat') !To create an output file
write (*,*)
do i=0,n
write (*,*) (nCr(i,j),j=0,n) !To display output on screen
write (1,*) (nCr(i,j),j=0,n) !To write output to file
end do
end program pascal_property
!To display Pascal's Triangle
program pascal_property
implicit none
integer i,j,n,middle
real m
integer, allocatable:: nCr(:,:)
write (*,*) "Give the value of n for the Pascal's Triangle"
read (*,*) n
write (*,*) "The value of n is= ",n
allocate (nCr(0:n,0:n))
!To initialize elements
do i =0,n
do j=0,n
if (j==0) then !To set all elements in the first column to 1 as nC0=1
nCr(i,j)=1
else if (i==j) then !To set all elements nCr where n=r to 1 as nCn=1
nCr(i,j)=1
else if (j.gt.i) then !To set elements not needed in the array to 0
nCr(i,j)=0
else
nCr(i,j)=-1 !To initialize the rest of the elements. Used -1 to differentiate from 1 and 0 used earlier.
end if
end do
end do
do i=0,n
m=i !It is important to give a real value for the ceiling function. Hence, we cannot use the variable 'i' directly. You can also use real (i) in the function instead
if (mod(m,2.0)==0) then !To calculate values for half of the triangle. 2.0 is used instead of 2. Arguments of modulus function must be of the same type (integer or real)
middle=ceiling(m/2)!To calculate the variable 'middle' based on simple logic
else
middle=floor(m/2)
end if
do j=0,i
if (nCr(i,j)==-1) then
if (j.le.middle) then
nCr(i,j)=nCr(i-1,j-1)+nCr(i-1,j)
nCr(i,i-j)=nCr(i,j) !To calculate elements in the remaining half using the property of symmetry, nCr=nC(n-r)
end if
end if
end do
end do
open (1,file='pasc.dat') !To create an output file
write (*,*)
do i=0,n
write (*,*) (nCr(i,j),j=0,n) !To display output on screen
write (1,*) (nCr(i,j),j=0,n) !To write output to file
end do
end program pascal_property
Sample Output from the file for n=8 |
No comments:
Post a Comment