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

I need help producing a pyramid with a certain algorithm to combine numbers (nested loops)

I have the layout for the pyramid set I just can't find out how I would combine or mathematically get the next few numbers. What I need is: 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 My code right now is: int x =…
DizzyOak
  • 27
  • 1
  • 4
1
vote
4 answers

Printing Pascals Triangle (recursive) (JAVA)

So far I have this, but I am not quite sure how printPTriangle can print the triangle using the code. If someone could help me out with this, I would be grateful. public static int factorial(int n) { if (n == 1) { return 1; } …
XxRUSxX
  • 15
  • 1
  • 5
1
vote
1 answer

variant of pascal's triangle in haskell - problem with lazy evaluation

To solve some problem I need to compute a variant of the pascal's triangle which is defined like this: f(1,1) = 1, f(n,k) = f(n-1,k-1) + f(n-1,k) + 1 for 1 <= k < n, f(n,0) = 0, f(n,n) = 2*f(n-1,n-1) + 1. For n given I want to efficiently get…
haselhorstk
  • 169
  • 1
  • 1
  • 7
1
vote
2 answers

Pascals triangle advantages

Pascal triangle can be used to calculate number of combinations using the recursion formula below:- (n k) = (n-1 k-1) + (n-1 k) This can be used in place of formal expansion of (n k) to calculate number of combinations. But both perform in O(n)…
1
vote
2 answers

Pascal Triangle in FORTRAN

I'm trying to create pascal triangle with FORTRAN. I did the algorithm. Compiled in C and succeeded but for some reason I'm not getting the same desired result in FORTRAN. Can anyone help me out in this one? Code in C (Working): #include…
Arman
  • 13
  • 1
  • 3
1
vote
1 answer

Wrong multidiemnsional-array initialization trying to build the pascal triangle (JAVA)

I am trying to write a class that creates an object of a pascal triangle using multidimensional arrays. Now i do have everything in place (at least i think so) except for the correct Initialization of the array. My program reads as follow: class…
1
vote
1 answer

Using raw_input in pascal's triangle

I was looking at a post by Don Marco that involved making Pascal's Triangle in python. I wanted to understand the code better so I tried played with it and tried to have it take a user input. This is the code I used: def triangle(rows): …
user3163789
  • 63
  • 2
  • 6
1
vote
5 answers

Pascal's Triangle using mainly functions in C++

I was trying to write a code that would display pascals triangle. Instead of displaying the result as : my result is displayed as 1 1 1 …
user2977810
  • 35
  • 1
  • 1
  • 4
1
vote
1 answer

Stuck with Pascal's Triangle in Java

Ok, I need to have a class that re-creates the Pascal Triangle. We are using BlueJ and I can't get my arrays to access each other. Here's the code: public class PascalTriangle { private int currentLineNumber; private int[]…
1
vote
3 answers

Pascal's Triangle in Ruby

I am writing Pascal's triangle in Ruby, but keep getting the error message: pascalsTriangle.rb:3:in 'triangle': undefined method `each' for 4:Fixnum (NoMethodError) from pascalsTriangle.rb:18 def triangle(n) for r in n: lst=[1] …
RuneScape
  • 75
  • 1
  • 7
1
vote
3 answers

Why am I getting Stack Overflow error in Recursive C program while computing elements of pascal triangle?

I am writing a C program to compute (i,j)th element in Pascular Triangle i.e f(n,1) = f(n,n) = n, and f(n,k) = f(n-1,k) + f(n-1,k-1) for 1 < k < n I need to print value modulo 1000000007. The Code follows : #include #include…
mkkhedawat
  • 1,687
  • 2
  • 17
  • 34
1
vote
5 answers

Pascal's triangle positioning

I made a Java program that prints out a pascal triangle, however I can't figure out how to correctly position it. Program 1 public class Triangle { public static void main() { System.out.println("\nTriangle: "); int row = 11; …
Noob
  • 113
  • 3
  • 11
1
vote
3 answers

prolog pascal triangle

hi is there anybody know how can i do the pascal nth row when i ask for :? pascal(2,Row). i get Row=[1,2,1] ?? please help me
thisnotmyname
  • 11
  • 1
  • 2
1
vote
2 answers

Pascal's triangle in Prolog

I have written a function for returning the next row in Pascal's triangle given the current row: pascal_next_row([X],[X]). pascal_next_row([H,H2|T],[A|B]):- pascal_next_row([H2|T],B), A is H + H2. I want to be able to find the nth row in…
blork
  • 2,150
  • 6
  • 26
  • 45
1
vote
2 answers

Lazy Pascal's Triangle in Clojure

I'm trying to write a succinct, lazy Pascal's Triangle in Clojure, rotated such that the rows/columns follow the diagonals of the triangle. That is, I want to produce the following lazy-seq of lazy-seqs: ((1 1 1 1 ...) (1 2 3 4 ...) (1 3 6 10…