pascal triangle

Status
Not open for further replies.

lemaa

Beta member
Messages
4
i solved it at high school in pascal lang. now after 8 years i've given it to my student to solve, but i myself forgot to consider about. please if one has the ready answer in pascal lang send it.
thanks.

if you don remember, the triangle looks like:
1
121
12321
1234321
...
...
 
use the mathamatical induction formula....thats the more accurate way, the easier way(uptill row 5) is multiples of 11. but thats true only till row 5.
 
this prints it in a weird way...but its a solution, MIND U....ITS USING MATHAmatical induction, so its goign to be accurate upto the nth row.

its a little raw, and very much of a base programmer, coz i wrote this when i started out with c.
-----------------------------------------------------------

//Program to print Pascal Triangle
#include<stdio.h>
#include<conio.h>
#include<math.h>


void main()
{
static unsigned long int n,k,i,j,g;
unsigned long int fact(unsigned long int );
void tri(unsigned long int ,unsigned long int );
clrscr();
printf("Enter number\n");
scanf("%ld",&n);
for(i=1;i<=n;i++)
{ g=i+1;
for(j=0;j<=i;j++)
{

k=fact(i)/(fact(i-j)*fact(j));
g--;
tri(k,g);

}

}


getch();
}
//to find factorial of numbers
unsigned long int fact(unsigned long int x)
{
unsigned long int i,j=1,k;
for(i=x;i>=1;i--)
{
j=j*i;
}
return (j);
}

//to draw the triangle
void tri(unsigned long int k,unsigned long int g)
{
printf("%9ld",k);
if(g==0)
printf("\n\n");

}
----------------------------------------------------------------------
 
Oh, the problem is printing Pascal's Triangle. Darn, it seems i_learn beat me to it!

What he did is use the binomial theorem, essentially. Don't know if that's allowed. What this means is that the k-th element of the n-th row of the triangle (n and k start at 0) is given by the binomial:

(n k) = n! / [k! * (n - k)!]

where "!" means factorial: n! = n*(n-1)*(n-2)*...*1. Example: 4! = 4*3*2*1 = 24. He implemented the factorial function using the classic recursive function definition.

Also, I think lemma wanted the solution in Pascal.
 
thanks a lot. from your loop method i found out this. think it's more plain for those like me who don't get maths that much.

var n,k,i,j:integer;
begin
write('enter PT length> ' );readln(n);
writeln('1');
for i:=2 to n do {row's length}
BEGIN
k:=i;
for j:=1 to i*2-1 do {column's length}
BEGIN
if j<=i then write(j) {till the row's legth digits go up}
else
begin
k:=k-1; write(k); {after row's length, digits go down}
end;
END;
writeln(''); {to get to the next row}
END;

readln;
end.
 
I'm not sure that produces the Pascal Triangle... Have you verified it? I think you just want to print 1,11,121,12321... But this is Pascal's Triangle.

Another interesting problem would be to write a program to write the triangle without using binomial coefficients or factorials. Just the standard "add up the 2 above values" rule.
 
but it prints
1
121
12321
..
isn't it the triangle?? and what's that adding 2 above values? please enlighten.
 
Nope, that's not the Triangle. Follow this link to see an image.

The first two rows are trivial: the very first is a 1, and the second is two 1s. To generate the remaining rows, you follow this simple rule: the value inside any hexagon is the sum of the two hexagons directly above it.

So for the third row, this is what we'd do. First hexagon only has a 1 above it, so it's a 1. Second hexagon has two 1s above it, so it becomes 1+1=2. And the third is a 1 also. You'll soon notice the hexagons on the edges are always one. It's only the hexagons in between that get their value from the sum of those above.

The fourth row: start with a 1, then you have 1+2=3, followed by 2+1=3, then a 1 again, so it'll look 1,3,3,1.

And so on and so forth for all the Triangle.
 
yep...thats how u supposed to do it on paper, only i didnt want to actually think of a way to add the previous 2 numbers, when i did it, i knew just arrays, and it was too much of a bother then. mebbe what u can do is write recursively, to add a and a[i+1], and then put it in a stack or something, and pop it and print it.....but thats too much of a bother, like i mentioned...if u want just the first 5 rows....just print the exponentail table for 11....
it follows to be true till row 5 ..i think
1 11^0
11 11^1
121 11^2 (so on so forth)
1331
14641
 
Status
Not open for further replies.
Back
Top Bottom