Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.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获取pow_Java_Math_Biginteger - Fatal编程技术网

Java使用BigInteger获取pow

Java使用BigInteger获取pow,java,math,biginteger,Java,Math,Biginteger,查找序列的最后十位数字,1^1+2^2+3^3+…+1000^1000 我正在使用Java。。我想我可以用modpow函数来解决这个问题。BigInteger是我的Java代码,但它不工作。我该怎么做 public static void main(String[] args) { BigInteger a; BigInteger b = null; BigInteger c = new BigInteger("10000000000"); for (int i

查找序列的最后十位数字,1^1+2^2+3^3+…+1000^1000

我正在使用Java。。我想我可以用modpow函数来解决这个问题。BigInteger是我的Java代码,但它不工作。我该怎么做

 public static void main(String[] args) {
    BigInteger a;
    BigInteger b = null;
    BigInteger c = new BigInteger("10000000000");
    for (int i = 0; i <= 1000; i++) {
        a = new BigInteger("" + i);
        b.add(a.modPow(a, c));
    }
    System.out.println(b);
}
我得到了NullPointerException的错误。。对不起,我的英语,谢谢

BigInteger b = null;

因此,在第一次迭代中,当您执行b.adda.modPowa,c;,b为空

我认为您有两个基本错误,首先您从未初始化b-这可能是

BigInteger b = BigInteger.ZERO;
然后需要分配b.adda.modPowa,c的结果;由于BigInteger add未进行适当修改,因此将添加到b。就是

b = b.add(a.modPow(a, c));
当我进行这两个更改时,我得到了输出

4629110846701
null不是零值。用0初始化变量,如下所示

BigInteger b = new BigInteger("0");
即使如彼得·彼得罗夫所说,这个问题可以用一个更简单的解决方案来解决,而不需要使用大整数

import java.math.BigInteger;

class Main {
    public static void main(String[] args) {
        BigInteger num = BigInteger.ZERO;
        for (int i = 1; i <= 1000; i++) {
            num = num.add(BigInteger.valueOf(i).pow(i));
        }
        BigInteger res = num.mod(new BigInteger("10000000000"));
        System.out.println(res);
    }
}
更有效的解决方案是从biginteger中获取,在非常大的数字上速度非常慢

import java.io.InputStream;

class Main {
    public static void main(String[] args) {
        long result = 0;
        long modulo = 10000000000L;

        for (int i = 1; i <= 1000; i++) {
            long temp = i;
            for (int j = 1; j < i; j++) {
                temp *= i;
                temp %= modulo;
            }

            result += temp;
            result %= modulo;
        }
        System.out.println(result);
    }

无需使用BigInteger即可解决此问题。BigInteger b=null;因此,您在b.add处获得NullPointerException。如果不使用BigInteger,如何解决该问题-我像biginger b=newbiginger0那样写这行;然而,它也不起作用。
import java.io.InputStream;

class Main {
    public static void main(String[] args) {
        long result = 0;
        long modulo = 10000000000L;

        for (int i = 1; i <= 1000; i++) {
            long temp = i;
            for (int j = 1; j < i; j++) {
                temp *= i;
                temp %= modulo;
            }

            result += temp;
            result %= modulo;
        }
        System.out.println(result);
    }
9110846700