Questions tagged [pascals-triangle]

Pascal's triangle - is a triangular array of binomial coefficients where the elements of the first row and column are equal to one, and all other elements are the sum of the previous element in the row and column.

Pascal's triangle - is a triangular array of binomial coefficients where the elements of the first row and column are equal to one, and all other elements are the sum of the previous element in the row and column.

T[i][j] = T[i][j-1] + T[i-1][j];

Example:

 1  1  1  1  1  1  1  1  1 
 1  2  3  4  5  6  7  8 
 1  3  6 10 15 21 28 
 1  4 10 20 35 56 
 1  5 15 35 70 
 1  6 21 56 
 1  7 28 
 1  8 
 1 

Iterative method of filling an array:

int n = 9;
// an array of 'n' rows
int[][] arr = new int[n][];
// iterate over the rows of the array
for (int i = 0; i < n; i++) {
    // a row of 'n-i' elements
    arr[i] = new int[n - i];
    // iterate over the elements of the row
    for (int j = 0; j < n - i; j++) {
        if (i == 0 || j == 0) {
            // elements of the first row
            // and column are equal to one
            arr[i][j] = 1;
        } else {
            // all other elements are the sum of the
            // previous element in the row and column
            arr[i][j] = arr[i][j - 1] + arr[i - 1][j];
        }
    }
}
261 questions
49
votes
14 answers

How to efficiently calculate a row in pascal's triangle?

I'm interested in finding the nth row of pascal triangle (not a specific element but the whole row itself). What would be the most efficient way to do it? I thought about the conventional way to construct the triangle by summing up the corresponding…
none
  • 11,793
  • 9
  • 51
  • 87
37
votes
23 answers

Code-golf: generate pascal's triangle

Generate a list of lists (or print, I don't mind) a Pascal's Triangle of size N with the least lines of code possible! Here goes my attempt (118 characters in python 2.6 using a trick): c,z,k=locals,[0],'_[1]' p=lambda n:[len(c()[k])and…
fortran
  • 74,053
  • 25
  • 135
  • 175
21
votes
6 answers

Convert normal recursion to tail recursion

I was wondering if there is some general method to convert a "normal" recursion with foo(...) + foo(...) as the last call to a tail-recursion. For example (scala): def pascal(c: Int, r: Int): Int = { if (c == 0 || c == r) 1 else pascal(c - 1, r -…
DennisVDB
  • 1,347
  • 1
  • 14
  • 30
20
votes
12 answers

Pascal's Triangle for Python

As a learning experience for Python, I am trying to code my own version of Pascal's triangle. It took me a few hours (as I am just starting), but I came out with this code: pascals_triangle = [] def blank_list_gen(x): while…
louie mcconnell
  • 701
  • 3
  • 7
  • 20
12
votes
8 answers

Pascal's Triangle Format

The assignment is to create Pascal's Triangle without using arrays. I have the method that produces the values for the triangle below. The method accepts an integer for the maximum number of rows the user wants printed. public static void…
Sidarth Shahri
  • 121
  • 1
  • 1
  • 3
8
votes
8 answers

C++ Pascal's triangle

I'm looking for an explanation for how the recursive version of pascal's triangle works The following is the recursive return line for pascal's triangle. int get_pascal(const int row_no,const int col_no) { if (row_no == 0) { return…
starcorn
  • 8,261
  • 23
  • 83
  • 124
7
votes
5 answers

Pascal's triangle 2d array - formatting printed output

I have a small assignment where I have to use a 2d array to produce Pascal's triangle. Here is my code, and it works. There is an extra credit opportunity if I display the triangle like so: (source: daugerresearch.com) However, my spacing is not…
anthony
  • 401
  • 3
  • 9
  • 20
6
votes
2 answers

Centering Pascal's Triangle Output in C++

I have successfully written a code to output Pascal's Triangle in a somewhat triangular shape using cout.width(total_rows - current_row), but it looks like this: 1 1 1 1 2 1 1 3 3 1 …
aedunn
  • 121
  • 1
  • 3
  • 8
6
votes
2 answers

Adding water to stack of glasses

I always wanted to know if there is any real-world application of Pascal's triangle than just coefficients of the binomial expansion. I tried to solve this problem: But, What if I am adding K units of water and wants to find a glass which has the…
user16657590
6
votes
3 answers

Tail-recursive Pascal triangle in Scheme

I started to read SICP recently, and I'm very interested in converting a recursive procedure into a tail-recursive form. For "one dimensional" situations (linear ones), like the Fibonacci series or factorial computation, it is not hard to do the…
Junjie
  • 469
  • 1
  • 4
  • 14
5
votes
2 answers

Question on the array generating sequence in Raku

I've come across this code at rosettacode my @pascal = [1], { [0, |$_ Z+ |$_, 0] } ... Inf; .say for @pascal[^4]; # ==> # [1] # [1 1] # [1 2 1] # [1 3 3 1] Inside the explicit generator block, I know how the individual operators like the list…
Lars Malmsteen
  • 738
  • 4
  • 23
5
votes
2 answers

Pascal Triangle Recursive Program optimization in C++

I have built recursive function to compute Pascal's triangle values. Is there a way to optimize it? Short reminder about Pascal's triangle: C(n, k) = C(n-1, k-1) + C(n-1, k) My code is: int Pascal(int n, int k) { if (k == 0) return 1; if (n == 0)…
JohnG
  • 2,109
  • 3
  • 16
  • 12
5
votes
3 answers

Pascal's Triangle via Recursion

Can someone tell me if my current code is even possible? I have to create Pascal's Triangle with an input without using any loops. I am bound to recursion. I have spent 3 days on this, and this is the best output that I can come up with. def…
Antman912
  • 65
  • 1
  • 5
5
votes
4 answers

How do I switch rows and columns in a 2D array?

I am working on a code that will create a visual Sierpinski triangle to 3D print, and in order for it to work I have to use a Pascal triangle algorithm that will create an array so I can use to tell my algorithm that will create my triangles where…
5
votes
5 answers

Pascal Triangle in Haskell

I'm new to Haskell and I really need some help! I have to write a program that includes a recursive function to produce a list of binomial coefficients for the power n=12 using the Pascal's triangle technique. I have some ideas in my head but…
Ceres Hilton
  • 61
  • 1
  • 3
1
2 3
17 18