Board index » delphi » Pascal's triangle

Pascal's triangle

does anyone out there  have a copy of pascal's triangle source code?
if so send to me..thanks
cyBR

 

Re:Pascal's triangle


Quote
{*word*104}natER <heylo...@hcc.hawaii.edu> wrote:
>does anyone out there  have a copy of pascal's triangle source code?
>if so send to me..thanks

No.  You know what a pascal triangle looks like (or you can find out)

Glenn Grotzinger
Web Page: http://www.geocities.com/Paris/3537
Writer of Excellent Training Manual known as the TP Tutorial.
You may find this material on the web page eventually.
Other interesting things will eventually exist as well.

Re:Pascal's triangle


To: {*word*104}natER <heylo...@hcc.hawaii.edu>

Hi
C> does anyone out there  have a copy of pascal's triangle source code?
C> if so send to me..thanks
Well, If you mean a code that shows a triangle on the screen: No Problem

---------

procedure triangle(x1,x2,y1,y2,z1,z2 : integer);
  begin
    line(x1,x2,y1,y2);
    line(y1,y2,z1,z2);
    line(z1,z2,x1,x2);
  end;

Then just put some coordinates in and .......

use:
-----

program test;

 uses
   crt, graph;

 var
   gd, gm : integer;

 {the procedure has to be here}

begin
  gd:=detect;
  initgraph(gd,gm,'');
  if graphresult <> grok then halt(1);

  triangle(100,200,300,400,50,50);

  readln;
  closegraph;
end.

-------

ciao  Jan

Re:Pascal's triangle


Quote
Jan Geske wrote:

> To: {*word*104}natER <heylo...@hcc.hawaii.edu>

> Hi
> C> does anyone out there  have a copy of pascal's triangle source code?
> C> if so send to me..thanks
> Well, If you mean a code that shows a triangle on the screen: No Problem

> ---------

> procedure triangle(x1,x2,y1,y2,z1,z2 : integer);
>   begin
>     line(x1,x2,y1,y2);
>     line(y1,y2,z1,z2);
>     line(z1,z2,x1,x2);
>   end;

> Then just put some coordinates in and .......

> use:
> -----

> program test;

>  uses
>    crt, graph;

>  var
>    gd, gm : integer;

>  {the procedure has to be here}

> begin
>   gd:=detect;
>   initgraph(gd,gm,'');
>   if graphresult <> grok then halt(1);

>   triangle(100,200,300,400,50,50);

>   readln;
>   closegraph;
> end.

> -------

> ciao  JanJan,

I think the original post was asking for a program to generate Pascal's Triangle.
Pascal's Triangle is a classic example of a recurrence relation that arranges
the binomial coefficients into the shape of a triangle.  The binomial coefficients
are a set of ordered coefficients of the terms in an expansion of a power of a
binomial.  For example:

  Binomial     Expanded                              Coefficients
-----------------------------------------------------------------
  (a+b)^0       1                                    1
  (a+b)^1       a + b                                1,1
  (a+b)^2       a^2 + 2ab + b^2                      1,2,1
  (a+b)^3       a^3 + 3(a^2)b + 3a(b^2) + b^3        1,3,3,1

Here is the top of Pascal's Triangle:

           1
          1 1
         1 2 1
        1 3 3 1
       1 4 6 4 1
     1 5 10 10 5 1
           .
           .
           .

Note that each position of the triangle holds the sum of the two elements
diagonally above it.  If the positions of each row of the triangle are
0..n (from left to right) where n is the number of the current row, then
the coefficient values C(row,position) in each position are calculated as
follows:

        C(n,0) = 1  and  C(n,n) = 1     For n >= 0
        C(n,k) = C(n-1,k) + C(n-1,k-1)  For n > k > 0

Since each row of the triangle is dependent on the row immediately above it,
this lends itself nicely to a recursive algorithm:

FUNCTION CalcCoefficient(CONST n : Integer; CONST k : integer ) : Integer;
  BEGIN
    IF (k=0) or (k=n)
    THEN
      result := 1
    ELSE
      result := CalcCoefficient( n-1, k )  + CalcCoefficient( n-1, k-1 );
  END;

PROCEDURE GenerateTriangle( CONST maxOrder : INTEGER );
  VAR
    currentCoefficient : Integer;
    order              : integer;
    term               : integer;
  BEGIN
    FOR order := 0 TO maxOrder DO
      FOR term := 0 TO order DO
        BEGIN
          currentCoefficient := CalcCoefficient( order, term );
          { now store, or print the current coefficient - whatever you want }
        END;
  END;

Note, recursion, as is often he case, is not the most efficient way to generate
Pascal's Triangle.  In this case, each row of the triangle is calculated repeatedl
for every higher order row.  This wastes a trememdous amount of processing.

Hope this helps,
Craig

--
-----------------------------------------------------------------------------
Craig Jackson               "No matter how great your triumphs or how
cjack...@cmsa.gmr.com        tragic your defeats -- Approximately one billion
Electronic Data Systems      Chinese couldn't care less."    -  Lazlo

Re:Pascal's triangle


In article <3193AC5E.6...@cmsa.gmr.com> in comp.lang.pascal.misc, Craig

Quote
Jackson <cjack...@cmsa.gmr.com> wrote:

>FUNCTION CalcCoefficient(CONST n : Integer; CONST k : integer ) : Integer;
>  BEGIN
>    IF (k=0) or (k=n)
>    THEN
>      result := 1
>    ELSE
>      result := CalcCoefficient( n-1, k )  + CalcCoefficient( n-1, k-1 );
>  END;

>Note, recursion, as is often he case, is not the most efficient way to generate
>Pascal's Triangle.  In this case, each row of the triangle is calculated
>repeatedl
>for every higher order row.  This wastes a trememdous amount of processing.

Note, however, that one can speed it up enormously by cacheing, if the
number of results is not too great.  In this case declare an array
Cache[1..n,1..n] and initialise it to zero (which cannot be a triangle
element).  Within CalcCoefficient, look up Cache[n, k]; if that is not
zero, it is the result, otherwise calculate as before (* and these
calculations will usually benefit from Cache *) and store the result in
Cache.

I used this trick in a recursive program to calculate the number of ways
of giving change; for GBP10 the program took about an hour without Cache
and a fraction of a second with Cache.  I've put a copy of this program
on my draft WWW Home Page (URL not yet known).

--
John Stockton, Surrey, UK.  J...@merlyn.demon.co.uk  Turnpike v1.12  MIME

Other Threads