题目描述:
定义阶乘 n! = 1 × 2 × 3 × · · · × n。
请问 100! (100 的阶乘)有多少个约数。
代码:
import java.math.BigInteger;
public class 阶乘的约数 {
public static void main(String[] args) {
long[] nums = new long[102]; //素因数记录表
//一个数可以分解为许多个质因数乘积的形式。 因为1不是质数,所以1没有质因数,所以素数表记录为0即可,阶乘从2分解开始。
for(int i = 2; i <= 100; ++i) {
//对每个数使用唯一分解定理。
int n = i;
for(int j = 2; j*j <= n; ++j) {
if(n % j == 0) {
while(n % j == 0) {
n /= j;
nums[j] ++;
}
}
}
//如果是质数上面的for循环不会执行的,因为上面的j不可能取到n
if(n > 1) {
nums[n] ++;
}
}
long ans = 1;
for(int i = 1; i < nums.length; ++i) {
if(nums[i] > 0) {
ans *= (nums[i] + 1);
}
}
System.out.println(ans);
// BigInteger sum = BigInteger.ONE;
// for(int i = 1; i <= 100; ++i) {
// sum = sum.multiply(BigInteger.valueOf(i));
// }
//算不出结果
// for(BigInteger i = BigInteger.ONE; i.compareTo(sum.sqrt().add(BigInteger.ONE)) < 0; i.add(BigInteger.ONE)) {
// if(i == sum.sqrt() && sum.remainder(i) == BigInteger.ZERO) {
// count.add(BigInteger.ONE);
// }else if(sum.remainder(i) == BigInteger.ZERO) {
// count.add(BigInteger.valueOf(2));
// }
// }
// System.out.println(count);
}
}