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
1
vote
1 answer

C : Dynamic spacing when printing values

I'm doing a homework assignment in C where part of it involves printing the values with certain spacing between each integer. For example, using Pascal's Triangle: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 …
ESM
  • 175
  • 10
1
vote
1 answer

Creating a dynamic array of arrays using pointers

I'm trying to create a dynamic array of arrays. So, for example, let's look at Pascal's Triangle: 1 11 121 1331 14641 ... ... This is basically an array of length N, that has at each index an array of i+1. How exactly do we set that up? I've tried…
ESM
  • 175
  • 10
1
vote
1 answer

How to grow and return a list from inside a recursive function in python

I have a recursive solution to Pascal's triangle, but it returns the row of the triangle requested, and not all of the rows preceding it. I would like to know if there is a way to scoop up all the rows as they are computed from the base case and…
1
vote
1 answer

Pascal's triangle pattern suddenly breaks

The following program prints Pascal's triangle. The program works fine for small input but if you pass the constructor a value greater than 13, the pattern breaks. I don't understand why and need help. Also comment on my coding style: /** Following…
Abhinav Chauhan
  • 1,304
  • 1
  • 7
  • 24
1
vote
1 answer

Pascal's triangle works but throws an Notices

Here is my script. The program can't find values in $tri array when I do $somma=$tri[$y]+$tri[$z];? I keep getting Notices, but why?
1
vote
1 answer

Writing Pascal's Triangle using big.Int int

I have some code for Pascal's Triangle using big.Int. How do I add the values? I get an error: invalid operation: PascalTriangle[r - 1][c - 1] + PascalTriangle[r - 1][c] (operator + not defined on struct) I am using a big.Int array so I cannot…
NewToGo
  • 83
  • 5
1
vote
2 answers

Program only works with inclusion of (side effects free) cout statements?

So I've been working on problem 15 from the Project Euler's website , and my solution was working great up until I decided to remove the cout statements I was using for debugging while writing the code. My solution works by generating Pascal's…
philly b
  • 21
  • 3
1
vote
1 answer

Does Python's decrement loop run slower than increment loop?

I was solving leetcode's pascal's triangle problem. I observed that both the for loops performed the same task separately but solving it with decrement loop gave me an inefficient solution. Given a non-negative index k where k ≤ 33, return the kth…
sagar_jeevan
  • 761
  • 5
  • 16
  • 34
1
vote
2 answers

pascal triangle in haskell nested lists

im trying to get the kth row in every list in the pascal list. For example pascal 4 would get the 4nd element in every row. So it would return 1,4,10,20... etc. I understand how to construct an infinite pascal list which is what outputs below, but…
helloworld
  • 181
  • 3
  • 12
1
vote
0 answers

Pascal triangle kth coefficient in nth row proof

I would like to know how the below formula holds for a pascal triangle coefficients. I didn't understand how we get the formula for a given row nck = (n-k+1/k) * nck-1 Note: if we know the previous coefficient this formula is used to calculate…
1
vote
1 answer

Pascal Triangles with Tuples in Recursion in Python

I am asked to write a recursion for Pascal's triangle, with tuples. This is my code: def pascal(n): if n == 1: return (1,) if n == 2: return ((1,),(1,1)) else: new_row = () for i in range(-1, n-1): …
aijnij
  • 91
  • 2
  • 10
1
vote
1 answer

Pascal triangle calculation without using for/while loop

I would like to generate Pascal pyramid data from a given data set that looks like this Pyramid(1,2,3,4,5,6,7,8,9); This is what I have been doing but it reaches only the second layer while I want it to recursively loop till the…
1
vote
3 answers

Pascal's triangle in C weird output

I'm trying to write a program to display Pascal's triangle up to a user-inputted number of levels. We aren't allowed to use the factorial method. My current code is this: #include void trianglePrint(const int numLevels); int main() { …
Ismael
  • 197
  • 1
  • 2
  • 12
1
vote
2 answers

Trinomial triangle (Python)

As part of my interest in learning Python, I've hit a stop when coming across an exercise that states: Consider the expression (1 + x + x^2)^n and write a program which calculates a modified Pascal’s triangle (known as the trinomial triangle)…
tsvenbla
  • 432
  • 4
  • 16
1
vote
3 answers

Recursive method that prints Pascal's triangle upside-down and rightside-up (via boolean)

I'm trying to create a recursive method that prints the Pascal's triangle both upside-down and rightside-up. I want to use a boolean variable to switch between an upside down triangle and a rightside-up triangle. So far, I have successfully written…
TheSaviour
  • 61
  • 1
  • 7