Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 避免使用BigInteger_Java_Algorithm_Math - Fatal编程技术网

Java 避免使用BigInteger

Java 避免使用BigInteger,java,algorithm,math,Java,Algorithm,Math,我有一个问题: f(N)是N!中尾随零之前的最后五位数字 例:11!=39916800,f(11)=99168 例:13!=6227020800,f(13)=70208 找到f(N),其中N是函数输入 我的解决方案是: public static String Solving(int n) { if (n > 10) { String val; BigInteger z = BigInteger.ONE; for (int i =

我有一个问题:

f(N)是N!中尾随零之前的最后五位数字

例:11!=39916800,f(11)=99168

例:13!=6227020800,f(13)=70208

找到f(N),其中N是函数输入

我的解决方案是:

public static String Solving(int n) {

    if (n > 10) {
        String val;
        BigInteger z = BigInteger.ONE;
        for (int i = 1; i <= n; i++) {
            z = z.multiply(BigInteger.valueOf(i));
        }
        val = String.valueOf(z);
        val = val.substring(n % 10);
        val = val.substring(0, 5);
        return val;
    } else return "";
}
公共静态字符串求解(int n){
如果(n>10){
字符串val;
BigInteger z=BigInteger.1;

对于(inti=1;i编辑:感谢用户58697和灰胡子的出色建议

首先,从1->n计算所有数字中因子5的个数

然后拆下所有2号和5号线对

最后,计算结果模量10^5

static long mod = 100000;
public static long Solving(int n) {
    int five = 0;
    for (int power5 = 5, count ; 0 < (count = n / power5) ; power5 *= 5){ 
        five += count; 
    }

    // Number of pair (2,5) is the min number between 2 and 5
    int removeFactorTwo = five;
    int removeFactorFive = five;
    long result = 1;
    for(int i = 2; i <= n; i++){
        int st = i;
        while(st % 2 == 0 && removeFactorTwo > 0){
            st /= 2;
            removeFactorTwo--;
        }
        while(st % 5 == 0 && removeFactorFive > 0){
            st /= 5;
            removeFactorFive--;
        }
        result *= st;
        // This will make sure result always <= 10^5
        result %= mod;
    }
    return result;
}
静态长模=100000;
公共静态长解算(int n){
int五=0;
对于(intpower5=5,count;0<(count=n/power5);power5*=5){
五+=计数;
}
//成对数(2,5)是介于2和5之间的最小数
int RemoveFactorWO=5;
int-removeFactorFive=5;
长期结果=1;
对于(int i=2;i 0){
st/=2;
去除因子2-;
}
而(st%5==0&&removeFactorFive>0){
st/=5;
去除因子五——;
}
结果*=st;

//这将确保结果始终可以使用
Long
@john,它将以更大的数字失败,例如f(90);实际上没有问题。那么BigInteger有什么问题呢?首先,将所有数字从1->n分解,然后删除所有2和5对,然后计算结果模10^5。这不会显示任何结果。为什么不删除
10^k
呢?@zlakad我不明白你的意思?限制是什么?不需要计算2:RE总是比FIVES更多。你不能认真地确定十移除的能力。除此之外,考虑<代码>(int Point 5=5,计数;0 <(计数=N/POWER 5);POWER 5*=5)五+=计数;< /代码>