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
5
votes
2 answers

Given an integer z<=10^100, find the smallest row of Pascal's triangle that contains z

How can I find an algorithm to solve this problem using C++: given an integer z<=10^100, find the smallest row of Pascal's triangle that contains the number z. 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 For example if z=6 => result is on the 4th…
quangh
  • 388
  • 4
  • 8
4
votes
3 answers

Best way to generate pascal's triangle (of two mentioned ways)

I have an programming assignment in Java. I have implemented it by making an nCr ( http://en.wikipedia.org/wiki/Combination ) function then using a double for loop to make the triangle by printing it out. However, the assignment calls for a uneven…
Portablejim
  • 824
  • 1
  • 12
  • 21
4
votes
2 answers

why is there a SIGFPE?

for some reason, it used to work. but now i get a SIGFPE.....what's wrong? #include "usefunc.h" long factorial(long num) { if (num > 1) { long counter; long fact = 1; for (counter = num; counter > 0; counter--) fact *=…
tekknolagi
  • 10,663
  • 24
  • 75
  • 119
4
votes
5 answers

Creating a Pascal triangle in a dictionary

I am trying to create a function that returns a dictionary that describes a pascal triangle. For example, pascal(3) would give me {1: [1], 2: [1,1], 3: [1,2,1]} I currently know how to create a function that returns the list of elements in a…
4
votes
2 answers

When trying to print pascals triangle 13th iteration prints wrong answer

I'm writing a script in C to print out pascals triangle, so I wrote a function for factorial, then made the variable c = to the binomial expansion equation, this works up until line n = 13 where it produces the output: 1 1 1 1 2 1 1 3 3 1 1 4 6…
Chadwick
  • 43
  • 4
4
votes
2 answers

Is there a more efficient way to write this recursive process?

I was asked to write a procedure that computes elements of Pascal's triangle by means of a recursive process. I may create a procedure that returns a single row in the triangle or a number within a particular row. Here is my solution: (define (f n) …
4
votes
3 answers

Calculating total of a row in pascal triangle?

I've been trying to calculate total of 1500th row in pascal triangle in c++. I tried more than 6 different code snippets from all over the web. Some of them crashed before 10th row, some gone crazy etc. How can one achieve calculating total of…
Max Abrahamsson
  • 1,460
  • 3
  • 17
  • 35
4
votes
4 answers

python recursive pascal triangle

After completing an assignment to create pascal's triangle using an iterative function, I have attempted to recreate it using a recursive function. I have gotten to the point where I can get it to produce the individual row corresponding to the…
Verbal_Kint
  • 1,366
  • 3
  • 19
  • 35
3
votes
1 answer

Why is this memoized Pascal's Triangle recursion not as fast as expected?

Referencing this article on Memoization, I believe that this approach uses memoization, and should be fast. However, it doesn't seem to be the case here: pascal :: Int -> Int -> Integer pascal a = (map ((map pascal' [0..]) !! a) [0..] !!) where…
my99n
  • 33
  • 6
3
votes
1 answer

Access previous parent value in Array.map()

I'm trying to create a function to create pascals triangle in javascript with Array.map() function. Actually, i'm using this function : let triangle = [], maxRows = 5 // Columns for (let i = 0; i < maxRows; i++) { // Create new row …
Thomas
  • 88
  • 7
3
votes
2 answers

pascal's triangle recursion

I need to write a Java-Code which prints the pascal's triangle. This is what I did until now. import java.util.Scanner; class Pascal { static int bnk (int n, int k) { if (k==0 || k==n) { return 1; } // B(n,k)…
Tim Buchholz
  • 83
  • 1
  • 3
  • 10
3
votes
4 answers

Pascal's Triangle in C

I'm a computer engineering student and next semester I am going to start C course. So in order to prepare myself a bit, I have started learning C by myself and stumbled across an interesting task, designed for, how it seemed to me at first sight,…
Leo
  • 33
  • 3
3
votes
3 answers

What's a more idiomatic and concise way of writing Pascal's Triangle with Clojure?

I implemented a naive solution for printing a Pascal's Triangle of N depth which I'll include below. My question is, in what ways could this be improved to make it more idiomatic? I feel like there are a number of things that seem overly verbose or…
maxcountryman
  • 1,562
  • 1
  • 24
  • 51
3
votes
6 answers

Pascal's Theorem for non-unique sets?

Pascal's rule on counting the subset's of a set works great, when the set contains unique entities. Is there a modification to this rule for when the set contains duplicate items? For instance, when I try to find the count of the combinations of the…
billjamesdev
  • 14,554
  • 6
  • 53
  • 76
2
votes
1 answer

Find all possible combinations of a scores consistent with data

So I've been working on a problem in my spare time and I'm stuck. Here is where I'm at. I have a number 40. It represents players. I've been given other numbers 39, 38, .... 10. These represent the scores of the first 30 players (1 -30). The rest of…
user678392
  • 1,981
  • 3
  • 28
  • 50
1
2
3
17 18