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

optimised pascals triangle with dynamic programming using python

def pascal(row, col): if col == 1 or col == row: return 1 else: return pascal(row - 1, col) + pascal(row - 1, col - 1) The above recursive implementation is exponential in terms of time complexity. With the knowledge of…
-1
votes
2 answers

MATLAB Recursive Pascals Triangle

I am learning about recursive functions. I want to be able to output a row vector 'n' of pascals triangle. k is the element of that row. For example, n = 3, row = 1 2 1. So far I have tried this, yet unsuccessful. The error I get is that I need to…
Amber
  • 43
  • 4
-1
votes
3 answers

pascal tringle using lambda without variables or recursion

Hi I have a task to use a lambda that print's a pascal tringle. The problem I'm having is that I can't use any variables nor can I use a recursive lambda. I need to submit my answer in the following way: lambda x : because the answer is…
Golden
  • 93
  • 9
-1
votes
4 answers

Pascal´s triangle c#

i´d like to build a pascal´s triangle as a square matrix in c# like this. 1 0 0 0 0 1 1 0 0 0 1 2 1 0 0 1 3 3 1 0 1 4 6 4 1 But the following code didn´t perform, could you please help me? Console.Write("Size of Matrix: "); int size =…
danbe
  • 1
-1
votes
3 answers

Pascal's Triangle gone wrong

I am trying to make Pascal's Triangle in JavaScript but there are lots of errors. I have know idea why errors are happening but there are some. Code: function triangle() { this.rows = [[1]]; this.genRow = function() { …
-1
votes
1 answer

different version of pascal triangle, list, python

I am a beginner in python and I have to do a special version of pascal triangle...special in a way that the elements don't add up but they multiply. here is the example: so the function is defined as multiplicative_pascal(start: int, height: int)…
Matus
  • 43
  • 5
-1
votes
2 answers

Recursive method causes java.lang.StackOverflowError

I am trying to create Pascal's Triangle, and the line: return pascalTriangle(row-1, col-1) + pascalTriangle(row-1, col); in the recursive method that returns int values in the Pascal's triangle, causes Exception in thread "main"…
dolnuea
  • 33
  • 1
  • 6
-1
votes
1 answer

TypeError: 'int' object is not subscriptable - Python3

I am trying to solve the Pascal's triangle problem in python3 but keep getting TypeError 'int' object is not subscriptable everytime. Here, the question is: Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. class…
vk7
  • 3
  • 2
-1
votes
2 answers

I am trying to generate list of lists to return pascals triangle

I am trying to generate pascal's triangle using powers of 11 but it works only till 4 and after 4 code needs to be modified to achieve further part of the triangle. Any leads for the answer(if possible through this method) are appreciated. class…
Venkatesh Gotimukul
  • 741
  • 1
  • 9
  • 30
-1
votes
1 answer

Pascal triangle maximum path

I am trying to solve problem 18 in project Euler. See here, https://projecteuler.net/problem=18. def maxpath(triangle): p = 0 total = 0 for x in range(0,len(triangle)): if p + 1 < len(triangle[x]) - 1: if…
Fahem Moz
  • 327
  • 3
  • 11
-1
votes
1 answer

I need a function to list all multiples of 3 in the previous output in decending order

I'm trying to participate in a coding challenge, and I have part of the code complete, but now I need a function to list all multiples of 3 in the previous output in descending order. Is there a function you could do that would run all the previous…
-1
votes
1 answer

How to get the adjacent blocks in Floyd triangle?

I need to write a function to find the adjacent blocks in Floyd triangle. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 What is…
bbnn
  • 3,505
  • 10
  • 50
  • 68
-1
votes
1 answer

Cannot Get Pascal's Triangle Recursive Program to Work --Java

I'm trying to write a program for an assignment. The requirements are to create Pascal's Triangle recursively and then print a given line. However, after compiling my program, I get several ArrayIndexOutOfBoundsExceptions. Here is the stack trace: …
-1
votes
2 answers

Output pascal's triangle using for-loops in C

I am trying to output a left-justified Pascal’s triangle. The program should first read one positive integer N from the user via a prompt “N:”. Then, the program will print the first N rows of the Pascal’s triangle on the screen. However, I think…
bow_one
  • 11
  • 1
  • 4
-1
votes
2 answers

error while generating pascal triangle

This is my code to generate Pascal's triangle in C language. #include #include void main() { int i, n, c; scanf("%d", &n); for (i = 0; i < n; i++) { for (c = 0; c <= (n - i - 2); c++) printf("…
shri_wahal
  • 344
  • 1
  • 4
  • 16