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

What's wrong with my pascal's triangle?

I've been looking for some simple coding challenges recently, and discovered about Pascal's triangle (here), and I've tried to generate one myself in C/Objective-C. For those that don't know what it is, that link explains it pretty well. I'm…
Alex Coplan
  • 13,211
  • 19
  • 77
  • 138
2
votes
3 answers

How to optimize Pascal's Triagnle in Python?

I have implemented the Pascal's Triangle in Python, it is pretty efficient, but it isn't efficient enough and there are a few things I don't like. The Pascal's Triangle is like the following: I have read this useless tutorial and this question, and…
2
votes
3 answers

Print Pascal's Triangle in Python properly

I have coded a Pascal's Triangle program in Python but the triangle is printing as a right angled triangle n=int(input("Enter the no. of rows: ")) for line in range(1, n + 1): c = 1 x=n y=line for i in range(1, line + 1): …
2
votes
1 answer

Pascal's triangle in Java

Java beginner here! As part of practicing programming, I've run into Pascal's triangle. I tried to implement a solution where the triangle is printed like so: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 ... So roughly right-sided. My solution though runs into…
Quant
  • 181
  • 1
  • 9
2
votes
3 answers

Pascal's triangle in C with combinations

#include long factorial(int num) { int counter; int fact = 1; for (counter = num; counter > 0; counter--) fact *= counter; return fact; } float combinations(int n, int k) { int numerator = factorial(n); int…
tekknolagi
  • 10,663
  • 24
  • 75
  • 119
2
votes
1 answer

More recursive method to print Pascal's triangle

I'll try to use a recursive method to print pascals triangle to the standard output. I started by making an iterative method to get an idea of ​​how I wanted the method to work. See the code below. /** * Print Pascal's triangle with PrintOut. * *…
2
votes
6 answers

Formatting Pascal's triangle

I am currently working on a homework assignment to generate what is known as Pascal's triangle in Python. So far, this is what I have: def mytri(myrange): trianglevar = [[1]] for i in range(1, myrange): tempvar = [1] for n in…
Python Newbie
  • 627
  • 1
  • 6
  • 5
2
votes
2 answers

Pascal's triangle faulty output

Hi I'm making an iterative Pascal's triangle in Java. So far everything works great, until number of rows exceed 13. The output becomes faulty. I must be doing something wrong here, please help. IterativePascal: public class IterativePascal extends…
radholm
  • 87
  • 10
2
votes
2 answers

Printing Pascal's triangle in Haskell

I just started learning Haskell, and have been doing a few problems online. Most of the times, I can figure out the solution but I am unable to print it out in the output format that is expected. For example, I tried doing a Pascal's triangle…
Master_Pabu
  • 23
  • 1
  • 4
2
votes
1 answer

Logic behind finding out Pascal's Triangle

I found a bit of code to obtain Pascal's triangle without using arrays or nCr in Java, given below: int maxRows = 6; int r, num; for (int i = 0; i <= maxRows; i++) { num = 1; r = i + 1; //pre-spacing for (int j = maxRows - i; j >…
Somenath Sinha
  • 1,174
  • 3
  • 16
  • 35
2
votes
2 answers

Pascal's Triangle with Binomial coefficient

I must write a predicate to compute a row of Pascal's triangle. I try to write this with Binomial coefficient. In a first time, I managed to do it by displaying each result with writeln(). But I would like to save them in my list L. How can I do…
Steve23
  • 113
  • 5
2
votes
1 answer

How to Generate the M-Arithmetic Triangle - Java

I'm looking to generate an M-Arithmetic triangle of order 4. It's described here: http://www.sciencedirect.com/science/article/pii/S0024379509003577?np=y And it looks like this: 1 1 1 1 1 1 2 3 4 3 2 1 1 3 6 10 12 12 10 6 3 1 1 4 10 20 31 40 44 40…
Thev
  • 1,105
  • 2
  • 13
  • 24
2
votes
13 answers

How to program Pascal's Triangle in Javascript - confusion re Arrays

I'm having a little trouble with my attempt at this problem. Code Below: function pasc(n){ var result = [[1]]; for (var row = 1; row < n; row++){ for (var col = 1; col <= row; col++){ result[row][col] = result[row - 1][col] + result[row…
Robin Andrews
  • 3,514
  • 11
  • 43
  • 111
2
votes
1 answer

Pascal's triangle in OOP

My teacher wrote us a tester class for his problem with Pascal's triangle. Here it is: public class TestTriangle { public static void main(String args[]) { PascalsTriangle triangle = new PascalsTriangle(7); if…
Erika
  • 31
  • 2
2
votes
1 answer

Can't Understand this line in Pascal's Triangle for C++

I have to write a code that generates a pascal's triangle with 12 rows. I've written everything on my own except one part, That's the formula we use to generate numbers. And the problem is I don't understand what's the connection between our…
user2604179
1 2
3
17 18