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
2
votes
2 answers

Why does my generator function always return the same value?

I want to build a generator for Bernoulli's triangle, the number i in the j line in the triangle is the partial sum of the first i numbers in pascal's triangle in line j. the triangle would look like this : which would be represented in python by…
Tam211
  • 723
  • 5
  • 10
  • 27
2
votes
2 answers

How to find number of times a number repeated in pascal's triangle?

Can anyone give an algorithm to find the number of times a number repeats in pascal's triangle? For example num - No of times 1 - infinite 2 - 1 3 - 2 4 - 2 . . 6 - 3 . . 10 - 4 . . for image Link Or in other way, how many nCr 's are possible…
nbbk
  • 1,102
  • 2
  • 14
  • 32
2
votes
2 answers

Pascal's Triangle Row Sequence

I'm currently working on finding the row sequences of Pascal's triangle. I wanted to input the row number and output the sequence of numbers in a list up until that row. For example, (Pascal 4) would give the result (1 1 1 1 2 1 1 3 3 1). I am…
Nopiforyou
  • 350
  • 1
  • 7
  • 20
1
vote
3 answers

Pascal's Triangle returning nonsense values

This is a homework project I was assigned some time ago... I've been successful in getting this far on my own, and the only hiccup I have left is (I believe) an issue with data types and overflow. I've tried changing over to unsigned and double, and…
WCStride
  • 35
  • 6
1
vote
3 answers

Java IndexOutOfBounds when trying to get a previous entry in a List

I'm working on a question where you are supposed return n rows of pascals triangle given and int n and return it as a List of Lists. However I'm getting an index out of bounds exception when I try to call on a previous row and I'm not exactly sure…
1
vote
0 answers

Android studio drawing rectangle

im new to app development and im working on my first android app and its gonna be a small app that will generate pascal's triangle (Wikipedia page). It generates an ArrayList of BigInteger arrays and i wanna show that as a triangle. How can i draw…
1
vote
2 answers

Printing Pascal Triangle in C

I've looked at some questions asked here regarding Pascal Triangle, yet I still can't seem to visualize how the algorithm would work. [1] [1 1] [1 2 1] [1 3 3 1] (3 comes from 1 + 2 on the previous row) [1 4 6 4 1] (4 comes from 1 + 3, while 6 comes…
bolakecil
  • 66
  • 1
  • 7
1
vote
2 answers

Pascal's Triangle with Recursion in JS - Explanation Asked

I have checked the other topics. It looks like these issues mainly concerns C++ and Python devs but wanted to ask to have a better understanding about recursion. A Pascal Triangle is a triangle that consists of 1's on the edges and the sum of the…
1
vote
3 answers

Python: printing 1 row of Pascal Triangle

New to learning python and am having some trouble understanding a solution provided? It has to do with Pascal Triangle and printing the rows when asking a user to "enter a row number" There were bits of the solution provided and the rest I fit in…
1
vote
1 answer

Pascal's Triangle in Racket

I am trying to create Pascal's Triangle using recursion. My code is: (define (pascal n) (cond ( (= n 1) list '(1)) (else (append (list (pascal (- n 1))) (list(add '1 (coresublist (last (pascal (- n 1)))))) )))) ;appends the list from pascal…
Jay
  • 13
  • 3
1
vote
2 answers

How do I make this into a Pascal's triangle instead of a Right triangle?

I need a code for making a Pascal's triangle. This code is for a Right triangle, but I need it to be a Pascal's triangle. It needs to have 10 rows and does have a gap in the middle of the Top and the Bottom. Can anyone please help me on this? for…
1
vote
0 answers

How can I produce the nth line of Pascal's triangle?

I am looking for a function that can produce the nth line of Pascal's triangle. I've found several solutions online but I don't quite fully understand them. Could someone explain this code block? I don't understand why unshift method wouldn't just…
1
vote
3 answers

Array of binomial coefficients

So, I have implemented the binomial coefficient public static int binomial(int n, int k) { if (k == 0) return 1; else if (k > n - k) return binomial(n, n - k); else return binomial(n - 1, k - 1) * n / k; } public…
1
vote
1 answer

Why is this Pascal Triangle implementation giving me trailing zeroes?

I tried to implement it recursively (iteratively seemed less elegant, but please do correct me if I am wrong).But the output seems to be giving me trailing zeroes and the first few rows are unexpected.I have checked the base cases and the recursive…
user8622672
1
vote
0 answers

Can't figure out how to change my code to solve a recursive problem

Print k-element subsets of an n element set (In essence n choose k) represent each subset as an array and skip B[0]. For example [0 1 0 1] means {1,3} to print. I believe my main problem is in my printSubsets() function because I am calling the…