Vous êtes sur la page 1sur 6

/*

* Write a program to compute the value of a given position in Pascal's


Triangle (See image).

The way to compute any given position's value is to add up the numbers to the
position's right and left in the preceding row. For instance, to compute the
middle number in the third row, you add 1 and 1; the sides of the triangle
are
always 1 because you only add the number to the upper left or the upper
right
(there being no second number on the other side).
The program should prompt the user to input a row and a position in the row.
The program should ensure that the input is valid before computing a value
for
the position.
*/


#include <iostream>
using namespace std;

unsigned long int pascal (int row, int col)
{
if ( col == 0 )
return 1;
else if ( row == 0)
return 1;
else if ( col == row )
return 1;

return ( pascal ( --row, --col ) + pascal ( row , ++col) );
// row gets decremented in first function call, and remains so for the
seccond
// col gets decremented at the first func. call, and has to be
incremented for the seccond

}

int main(int argc, char** argv)
{ cout << endl<<endl;

cout << "\n Pascal's triangle: ";
cout << "\n 1 ";
cout << "\n 1 1 ";
cout << "\n 1 2 1 ";
cout << "\n 1 3 3 1 ";
cout << "\n 1 4 6 4 1 ";
cout << "\n 1 5 10 10 5 1 ";

int row, col;
row = 0;
col = 0;
do
{
cout << "\n \n please enter the row (starting from 0) : "; cin >> row;
cout << "\n please enter the column (starting from 0) : "; cin >> col;
}
while ( row < 0 || ( col > row ) );

cout << "\n \n the pascal triangle value is : "<< pascal ( row, col )
<<" have a nice day";



cout << endl<<endl;
return 0;

}


#include <iostream>
#include <map>

typedef unsigned long RetType;
typedef unsigned int ArgType;

RetType pascal (ArgType row, ArgType col)
{
typedef std::pair<ArgType, ArgType> Pos;
typedef std::map<Pos, RetType> PascalCache;

static PascalCache pascal_cache;

if ( col == 0 ) return 1;
if ( row == 0 ) return 1;
if ( col == row ) return 1;

PascalCache::const_iterator it;
Pos cur_pos(row, col);
RetType ret;

it = pascal_cache.find(cur_pos);

if (it != pascal_cache.end())
ret = it->second;
else
{
ret = pascal ( --row, --col )
+ pascal ( row, ++col );

pascal_cache[cur_pos] = ret;
}

return ret;
}

int main()
{
std::cout << pascal(32, 16) << std::endl;
}

The Pascal's triangle contains the Binomial Coefficients C(n,k); There is a very convenient
recursive formula
C(n, k) = C(n-1, k-1) + C(n-1, k)
You can use this formula to calculate the Binomial coefficients.
I am sorry but I didn't understand
:( can you show me the code
please? user2205090 Mar 24
'13 at 18:11


@user2205090: Did you actually bother to take a look at the wikipedia link
I gave? See the last section. Armen Tsirunyan Mar 24 '13 at 18:29


I would use the usual direct formula given in the section #Multiplicative
formula in the same article. Can easily be implemented with one or
two for loops. Decide if you want to use integer or floating-point
arithmetic for the calculation. They have different limitations. Jeppe Stig
Nielsen Mar 24 '13 at 18:47


@JeppeStigNielsen: The problem with the multiplicative formula is that
intermediate results may not be storable in your destination type whereas
the final reault is. That needs additional care. Armen Tsirunyan Mar 24
'13 at 18:56


Agree. You should probably use the formula C(n, k) == ( n*(n-
1)*(n-2)*... )/( k*(k-1)*(k-2)*... ) where both the numerator
and the denominator have k factors. If you calculate the entire numerator
first with an integer type (possibly BigInteger) by a for loop, then the
eventual division will be exact, of course. Jeppe Stig Nielsen Mar 25 '13
at 8:29


using System;
using System.Collections.Generic;

public class Program
{
public void Main()
{
for(int i =0 ; i<5;i++)
{
int sum = 1;
Console.WriteLine();
for(int j =0 ; j<=i;j++)
{
Console.Write(pascal(i,j));
//Console.Write(sum); //print without recursion
sum= sum *(i-j) / (j + 1);
}
}
}

public int pascal(int x, int y)
{
if((x+1)==1 || (y+1)==1 || x==y)
{
return 1;
}
else
{
return pascal(x-1,y-1)+ pascal(x-1,y);
}
}
}

How Pascal's Triangle is Constructed

At the tip of Pascal's Triangle is the
number 1, which makes up the 0
th
row.
The first row (1 & 1) contains two 1's,
both formed by adding the two numbers
above them to the left and the right, in
this case 1 and 0. All numbers outside
the triangle are considered 0's. Do the
same to create the 2nd row: 0+1=1;
1+1=2; 1+0=1. And the third: 0+1=1;
1+2=3; 2+1=3; 1+0=1. In this way, the
rows of the triangle go on forever. A
number in the triangle can also be found
by nCr (n Choose r) where n is the
number of the row and r is the element
in that row. For example, in row 3, 1 is
the 0
th
element, 3 is first element, the
next three is the 2
nd
element, and the last
1 is the 3
rd
element. The formula for nCr
is:


The factoral symbol ! means the
positive integer multiplied by all the
positive integers that are smaller than it.
Thus
5! = 5 4 3 2 1 = 120.

The Sums of the Rows
The sum of the numbers in any row is equal to 2 to the n
th
power or 2
n
, when n is the number of
the row. For example:

2
0
= 1
2
1
= 1+1 = 2
2
2
= 1+2+1 = 4
2
3
= 1+3+3+1 = 8
2
4
= 1+4+6+4+1 = 16
#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>

void genPyrN(int rows) {
if (rows < 0) return;
// save the last row here
std::vector<int> last(1, 1);
std::cout << last[0] << std::endl;

for (int i = 1; i <= rows; i++) {
// work on the next row
std::vector<int> thisRow;
thisRow.reserve(i+1);
thisRow.push_back(last.front()); // beginning of row
std::transform(last.begin(), last.end()-1, last.begin()+1,
std::back_inserter(thisRow), std::plus<int>()); // middle of row
thisRow.push_back(last.back()); // end of row

for (int j = 0; j <= i; j++)
std::cout << thisRow[j] << " ";
std::cout << std::endl;

last.swap(thisRow);
}
}

Vous aimerez peut-être aussi