Project Euler: Problem 53 – Combinatoric selections

Problem 53:

There are exactly ten ways of selecting three from five, 12345:
 
123, 124, 125, 134, 135, 145, 234, 235, 245, and 345
 
In combinatorics, we use the notation, 5C3 = 10.
 
In general,

nCr =
n!
r!(n-r)!
,where r <= nn! = nx(n-1)x…x3x2x1, and 0! = 1.

It is not until n = 23, that a value exceeds one-million: 23C10 = 1144066.
 
How many, not necessarily distinct, values of  nCr, for 1 <= n <= 100, are greater than one-million?

Idea:

I recognized this as another repeat of Binomial Coefficients as seen in Problem 15. I just needed to plug in the numbers in a nested loop and count how many times the answer was greater than 1 million.

int answer = 0;

final BigInteger ONE_MILLION = BigInteger.valueOf(1000000);

for(int n = 1; n <= 100; n++) {
   for(int r = 1; r <= n; r++) {
      if(ONE_MILLION.compareTo(EulerUtils.nChooseM(n, r)) < 0) {
         answer++;
      }
   }
}
Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *