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

How can i Resize Triangle shape using Jquery

i want to resize triangle using jQuery.I know about jQuery resizable function but in triangle shape how can i resize it. Here is code for triangle i want to resize it using mouse .triangle { width: 0px; height: 0px; border-style: solid; …
1
vote
3 answers

How to convert generic sequence into triangle using LINQ?

If I have a sequence IEnumerable (not numbers, just T): [ a, b, c, d ] How to return sort of Pascal's or Floyd's triangle: a ab abc abcd so it would be IEnumerable>? Wondering whether there a way to achieve this elegantly using…
abatishchev
  • 98,240
  • 88
  • 296
  • 433
1
vote
1 answer

Multiple time complexity solutions for recursive Pascal triangle algorithm?

I have created the following simple algorithm in Java that computes the Pascal triangle in a recursive way, in the form of a 2D list of ints: public class PascalTriangleRec{ private final int[][] points; public PascalTriangleRec(int size){ …
user3423641
  • 25
  • 1
  • 6
1
vote
2 answers

How to solve Pascal triangle loop fails issue?

I'm facing an error on my Pascal triangle, but I don't know whether it's Java or my code which is the problem. Here is the code: import java.util.Scanner; public class sdz { public static void main(String[] args) { Scanner sc = new…
Sdmg15
  • 19
  • 7
1
vote
1 answer

How to find a particular row of a pascals triangle in python?

Here is a snapshot of my code to write a pascal's triangle up to n rows into a file named "pascalrow.txt" after which it takes a row number as an input, and if that row is found, it reopens the file, finds the row number and displays the whole…
1
vote
3 answers

How do you find which row and column a number belongs to in Floyd Triangle

How do you find which row and column does a number belongs to 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…
bbnn
  • 3,505
  • 10
  • 50
  • 68
1
vote
2 answers

Pattern matching not matching given variable

I have made an implementation of the pascal's triangle but something is wrong with it since when col match { case row => ...} it doesn't correctly match the col with the row: def main(args: Array[String]) { println("Pascal's Triangle") for (row <- 0…
Cris
  • 2,002
  • 4
  • 30
  • 51
1
vote
1 answer

Recursive Pascals Triangle Layout

So i've managed to get Pascals Triangle to print successfully in terms of what numbers are printed, however, i can't get the formatting correct using: n = int(input("Enter value of n: ")) def printPascal(n): if n <= 0: #must be positive…
Staylor742
  • 11
  • 4
1
vote
2 answers

Pascal's Triangle - type error

So my code for some reason is giving me the error: TypeError: Can't convert 'int' object to str implicitly It has to do with the line: answer = answer + combination(row, column) + "\t" Here is my code: def combination(n, k): if k == 0 or k ==…
kneesarethebees
  • 85
  • 1
  • 2
  • 10
1
vote
1 answer

Pascal's triangle giving only last row using Java ArrayList

I am trying to generate the pascal's triangle for a lines. For 5 it gives me only the 5th row five times. Dont know why? public class Solution { public ArrayList> generate(int a) { ArrayList internal = new…
david419
  • 435
  • 3
  • 8
  • 18
1
vote
1 answer

Trying to use a while loop to keep looping until user puts in n (Java)

I'm trying to use a while loop to keep on asking the user if they want a certain row of the pascal's triangle. I don't know where to put my while loop. I want to ask Another(y/n)? and if the user puts in y I ask for Which line number of pascal's…
Anonymous
  • 43
  • 1
  • 8
1
vote
1 answer

How do you print within a for loop without starting a new line?

I have written some code for a program which will find Pascal's triangle. However, the formatting is not coming out as I would have hoped. My code is as follows: def factorial(n): """Calculate a factorial""" p = 1 for i…
1
vote
2 answers

print white space instead of zero for 2D int array in java

I am trying to print pascal's triangle using 2D int array And printing 2D array in below way public static void pascal (int n) { int[][] pascalArray = new int[n][n]; // Code here } printArray(pascalArray); …
user4549111
1
vote
1 answer

Sum of Pascal's Triangle rows

I implemented an algorithm for finding sums of Pascal's Triangle rows, but it's slowly for the contest. My program passed 4 test cases, but failed in the next case with a runtime error. Can I make my code faster? import math n = int(input()) for i…
Bob Napkin
  • 566
  • 6
  • 15
1
vote
1 answer

Design the binomial coefficient algorithm using a single dimensional array

I have already designed the following algorithm that determines the binomial coefficient using a two dimensional array. For example, to calculate the binomial coefficient of n choose k, we can create a two dimensional array like so: int[][] arr =…
AdamMc331
  • 16,492
  • 10
  • 71
  • 133