2

Can anyone give an algorithm to find the number of times a number repeats in pascal's triangle? For example

num - No of times

1 - infinite
2 - 1
3 - 2
4 - 2
.   .
6 - 3
.   .
10 - 4
.   .

for image Link

Or in other way, how many nCr 's are possible for nCr = x , where x is any given integer?

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
nbbk
  • 1,102
  • 2
  • 14
  • 32

2 Answers2

1

Just count. You know n > 1 can only appear in the first n+1 rows of Pascal's triangle. And that each row is symmetric, and increasing (for the first half). That saves time.

See http://oeis.org/A003016 for more about the sequence

Colonel Panic
  • 132,665
  • 89
  • 401
  • 465
0

I had to write something similar for a hackathon challenge. This code will find all the numbers 1 to MAX_NUMBER_TO_SEARCH that have a count of more than MINIMUM_COUNT in the Pascal Triangle of size PASC_SIZE. You can obviously alter it to only count for a single number. Not super efficient obviously.

function pasc(n) {
     var xx = [];
     var d = 0;
     var result = [];
     result[0] = [1];
     result[1] = [1, 1];
     for (var row = 2; row < n; row++) {
         result[row] = [1];
         for (var col = 1; col <= row - 1; col++) {
             result[row][col] = result[row - 1][col] + result[row - 1][col - 1];

             result[row].push(1);
         }
         for (var ff = 0; ff < result[row].length; ff++) {
             xx[d++] = (result[row][ff]);
         }
     }
     return xx;
 }

 function countInArray(array, what) {
     var count = 0;
     for (var i = 0; i < array.length; i++) {
         if (array[i] === what) {
             count++;
         }
     }
     return count;
 }

 var MAX_NUMBER_TO_SEARCH = 5000;
 var MINIMUM_COUNT = 5;
 var PASC_SIZE = 1000;

 var dataset = pasc(PASC_SIZE);
 for (var i = 0; i < MAX_NUMBER_TO_SEARCH; i++) {
     if (countInArray(dataset, i) >= MINIMUM_COUNT) {
         console.log(i + " Count:" + countInArray(dataset, i) + "\n");
     }
 }
David
  • 57
  • 1
  • 9