|  |
10-17-2006, 02:08 AM
|
#1 (permalink)
|
Newb Techie Join Date: Oct 2006 Posts: 4
| pascal triangle 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
...
... |
| |
10-17-2006, 09:07 AM
|
#2 (permalink)
|
Ultra Techie Join Date: Jul 2006 Posts: 714
| I don't get it, what's the problem to solve?
__________________ SuperPi 1M: 29.6s - 3DMark06: 8616 The Geek Test v3.1 Score: 36.3% - Major Geek |
| |
10-18-2006, 10:32 PM
|
#3 (permalink)
|
True Techie Join Date: Nov 2005 Posts: 115
| 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. |
| |
10-18-2006, 10:35 PM
|
#4 (permalink)
|
True Techie Join Date: Nov 2005 Posts: 115
| 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");
}
---------------------------------------------------------------------- |
| |
10-19-2006, 09:45 AM
|
#5 (permalink)
|
Ultra Techie Join Date: Jul 2006 Posts: 714
| 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.
__________________ SuperPi 1M: 29.6s - 3DMark06: 8616 The Geek Test v3.1 Score: 36.3% - Major Geek |
| |
10-20-2006, 01:38 AM
|
#6 (permalink)
|
Newb Techie Join Date: Oct 2006 Posts: 4
| 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. |
| |
10-20-2006, 01:45 AM
|
#7 (permalink)
|
Ultra Techie Join Date: Jul 2006 Posts: 714
| 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.
__________________ SuperPi 1M: 29.6s - 3DMark06: 8616 The Geek Test v3.1 Score: 36.3% - Major Geek |
| |
10-20-2006, 07:36 AM
|
#8 (permalink)
|
Newb Techie Join Date: Oct 2006 Posts: 4
| but it prints
1
121
12321
..
isn't it the triangle?? and what's that adding 2 above values? please enlighten. |
| |
10-20-2006, 10:14 AM
|
#9 (permalink)
|
Ultra Techie Join Date: Jul 2006 Posts: 714
| 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.
__________________ SuperPi 1M: 29.6s - 3DMark06: 8616 The Geek Test v3.1 Score: 36.3% - Major Geek |
| |
10-23-2006, 10:57 PM
|
#10 (permalink)
|
True Techie Join Date: Nov 2005 Posts: 115
| 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[i] 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 |
| |  | | Thread Tools | | | | Display Modes | Linear Mode |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | |