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

Creating a non-right Pascal's triangle (centered) in python

I need to write a code that inputs a non-right Pascal's triangle given the nth level as an input where the first row is the 0th level. Apart from that, at the end of each row the level must be indicated. Here's what I've made so far: level =…
Ram24
  • 43
  • 1
  • 9
-2
votes
2 answers

What is the best way to avoid printing an infinite amount of times?

I am trying to ask for the user input and I am unable to get it to work. I know there is a way to do this without functions, but I want to have my main function separate from the algorithm. I want the output to display the correct level…
user18839768
-2
votes
2 answers

Explain the working of this pascal's algorithm

1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 def pascal(n): if n == 1: return [1] else: line =…
Alpha
  • 9
  • 3
-2
votes
1 answer

Why can't 'return' return the value from the function?

Here is my code: def getRow(rowIndex): if rowIndex == 0: return [1] if rowIndex == 1: return [1,1] def f(n,r): if n == 1: print(r) return r r = [1]+[r[i] + r[i+1] for i in…
Hang Lin
  • 51
  • 4
-2
votes
2 answers

Python Pascal's Triangle Recursion Program

I'm trying to write a program to print a pascal's triangle. Here's my code: def combination(n, k): if k == 0 or k == n: return str(1) else: return combination(str(n-1, k-1)) + combination(str(n-1,k)) def…
Mikona
  • 13
  • 1
  • 5
-2
votes
1 answer

How to convert this recursive function into an iterative version?

This code basically computes nCr to print a pascal's triangle. #include int nCr(int n,int r){ if (r == 0 || r == n || n == 1 || n == 0){ return 1; } else{ return nCr(n-1,r) + nCr(n-1,r-1); } } How would this…
-2
votes
1 answer

How can I invert the result of this code in clojure?

(defn exp [n] (if (= n 0) 1 (* 11 (exp (dec n))))) (defn Pascals [n] (loop [x n] (when (< 0 x) (println (exp (- x 1))) (recur (- x 1))))) I need something like the Pascal's triangle from…
-2
votes
1 answer

can you tell me a little change in my own written pascal logic

I have just take the format and spacing but can any one tell me a single change to get the values which print in actual pascal triangle program in java... /* * To change this license header, choose License Headers in Project Properties. * To…
Love Poet
  • 19
  • 1
  • 1
  • 7
-2
votes
1 answer

Same Java code working on one computer and not on the other

I wrote a code in Java to generate a Pascal's Triangle. I have two laptops at home, one with Windows Vista Home Basic and the other with Windows 8.1 SingleLanguage, both having the same version of java loaded. The below mentioned code works…
-2
votes
1 answer

Please explain the following Pascal's Triangle Code

In the provided code for Pascal Triangle I would really appreciate if one could help me clear the following doubts. 1 public class Pascal { 2 3 static void calculatePascal (int[][] t) { 4 for (int i=0; i
meowthecat
  • 51
  • 8
-2
votes
1 answer

Printing Reverse Triangle with Alphabets

I have to print the following pattern in C# - A B C D E F G F E D C B A A B C D E F E D C B A A B C D E D C B A A B C D C B A A B C B A A B A A I'm able to print the pattern in C but not in C#…
user1547554
  • 441
  • 2
  • 8
  • 16
-2
votes
1 answer

Pascal's triangle series in php

I am trying to get pascal's triangle using two dimensional array. But it not works. can anyone solve this program. Thanks in advance.
Enter the number…
CJ Ramki
  • 2,620
  • 3
  • 25
  • 47
-2
votes
1 answer

Pascal's Triangle with Gray's theory

I am trying to create Pascal's Triangle with VB. Here's my code (It uses Gray's Theroy) : Dim input As Integer = Val(TextBox1.Text) Dim rownumber As Integer = 0 Dim columnumber As Integer = 1 Dim x As Integer = 1 Do Until rownumber = input Do…
Ds.109
  • 714
  • 8
  • 16
  • 32
-3
votes
4 answers

printing a pascal triangle in C

void Pascal(int n){ int i,j; int a[100], b[100]; a[0]= 1; for(i = 0; i <= n; i++){ printf(" "); b[i]=1; for(j = 0; j <= i; j++){ if (j <= 1) a[j-1]=0; b[j] = a[j-1] + a[j]; …
-3
votes
2 answers

Printing Equilateral Triangle A-Z Using Recursion C++

Ok, I just can't figure this problem out. I have to print out any size triangle in characters inputting two letters. Ex: range(c, j) and it has to be recursive. I can't have any loops in my code. Output is supposed to look like this: A …
lisabits
  • 1
  • 1
  • 8
1 2 3
17
18