从五个数12345中选出三个数一共有十种方法:
123, 124, 125, 134, 135, 145, 234, 235, 245, and 345
在组合数学中我们用5C3 = 10来表示.
概括来说:
nCr = |
n!
r!(nr)! | ,其中r n, n! = n(n1)...321, 并且0! = 1. |
n = 23时产生第一个超过一百万的数: 23C10 = 1144066.
对于nCr, 1 n 100,有多少超过100万的值?包括重复的在内。
import java.math.BigInteger;
public class Problem53
{
public static void main(String[] args)
{
long start = System.currentTimeMillis();
System.out.print("answer: ");
howmany();
long end = System.currentTimeMillis();
System.out.print("time: ");
System.out.println(end - start);
}
static void howmany()
{
int sum = 0;
for (int i = 23; i <= 100; i++)
{
for (int j = 3; j < i; j++)
{
BigInteger t = jiecheng(i).divide( jiecheng(j).multiply( jiecheng(i - j) ) );
if (t.compareTo(BigInteger.valueOf(1000000)) == 1)
{
sum = sum + (i - 2 * j + 1);
break;
}
}
}
System.out.println(sum);
}
static BigInteger jiecheng(int n)
{
BigInteger sum = BigInteger.ONE;
for (int i = 2; i <= n; i++)
{
sum = sum.multiply(BigInteger.valueOf(i));
}
return sum;
}
}
answer: 4075
time: 47