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
0
votes
3 answers

"application: not a procedure" while computing binomial

I am defining a function binomial(n k) (aka Pascal's triangle) but am getting an error: application: not a procedure; expected a procedure that can be applied to arguments given: 1 arguments...: 2 I don't understand the error…
Mary Martinez
  • 314
  • 5
  • 12
0
votes
1 answer

Formatting Pascal's Triangle in Python

I'm taking my first programming class and I'm a little lost as to how to finish off this code. I'm trying to construct a Pascal's triangle with n rows. I got the lines to print, each on their own row, but I'm having trouble formatting them into a…
JShell
  • 624
  • 2
  • 7
  • 22
0
votes
1 answer

Why doesn't this Pascal's Triangle program work?

Row is user-inputted. cout << "Input the number of rows: "; cin >> row; column=row; int triangle[row][column]; for (i=0;i<=row;i++){ for (j=0;j<=column;j++){ triangle[i][j]=0; } } for (i=0;i<=row;i++){ for (j=0;j<=i;j++){ …
user2027369
  • 63
  • 1
  • 7
0
votes
4 answers

pascal triangle proper formatting java

so I'm currently working on an assignment that I just can't seem to finish. Well I have everything finished but would like the extra credit. I've been looking around the web and can't really seem to find exactly what I'm looking for. public class…
kevorski
  • 816
  • 1
  • 11
  • 29
0
votes
2 answers

Find each number in pascal triangle on 1500th row?

I just asked another question about pascal triangle about finding sum of 1500th row. I'm so glad that people answered so quickly, But unfortunately later i realized, i need each individual number on 1500th row. Here i found an easy way to calculate…
Max Abrahamsson
  • 1,460
  • 3
  • 17
  • 35
0
votes
3 answers

Why does my BigInteger.add() get a NullPointerException?

I'm trying to make a Pascal Triangle print to a hundred rows, but Java's int seems to return negative values. I'm trying to use BigInteger, but I get NullPointerException whenever I add two numbers! I think I initialized them. Here is my code: …
0
votes
2 answers

Pascals triangle in one loop

Is it possible to write the pascal's triangle by using one loop ? I have written it by using more than one loop and it is working fine.
mohammedsuhail
  • 701
  • 2
  • 11
  • 23
-1
votes
1 answer

Weird (to me) behavior of increment (+=) operator for lists

The following code works and gives L as the first N lines in Pascal's triangle: N = 5 L = [[]] * N i = j = 0 while i < N: if j == 0 or j == i: a = 1 else: a = L[i-1][j] + L[i-1][j-1] L[i] = L[i] + [a] # *** if j < i: j +=…
Luiz Eleno
  • 13
  • 3
-1
votes
1 answer

Why is there a segmentation-fault error only with certain input values on pascal's triangle? (C)

I am trying to create a program outputting the pascal's triangle, using C in OnlineGDB and repl.it using a 2x2 array asking how many lines the user wants to print. The main problem is that the program works, but only until printing for 7 rows in…
-1
votes
1 answer

Generator for Pascals triangle

I made a generator of Pascals triangle values in list import math def combination(n, r): t = int((math.factorial(n)) / ((math.factorial(r)) * math.factorial(n - r))) return t def pascal_triangle(): …
Curly0606
  • 9
  • 2
-1
votes
1 answer

pascalTriangle and Venn Diagrams

I have downloaded results from scopus of papers by doing various queries for each topic. The downloaded singleton papers are identified by EID. I would like to see where the intersections are, i.e., if there are any overlaps (e.g., if those found…
-1
votes
2 answers

Can someone write the code for n = 19 rows and get this following figure? For Python

Write a function that accepts a number n as an input, and it returns n rows that look like the following pattern. Run your function for n = 19 (the output below is for n=19). n = int(input("enter number of rows:")) for i in range(1, n+1): for j…
user16864451
-1
votes
1 answer

Question about Pascal's Triangle generator

I am a Python beginner and I have written a code for generating "Pascal's Triangle" from math import factorial n = 50 for i in range(n): for j in range(n-i+1): print(end=" ") for j in range(i+1): …
-1
votes
1 answer

Pascal Triangle Find position x, y with Haskell function

I want to get 2 integers from an input like pascal 2 1. This input should be 2, because the list starts with x and y = 0. Also pos must be <= row and i don't want to use guards. My code looks like this: pascal :: Int -> Int -> Int pascal row pos …
Plex
  • 1
  • 1
-1
votes
1 answer

c sum of the squares of all the coefficients in pascals triangle in random row

So I´ve been trying to solve this problem, but I just can´t. The thing is, I put a number of row in triangle, (can be any random number/row) and the program should return the sum of the squares of all the coefficients in that row. #include…