我使用Bigintegers的简单计算不起作用[重复]

时间:2022-02-27 17:13:41

This question already has an answer here:

这个问题在这里已有答案:

I need to calculate this: 2894135^3787313 mod 4028033

我需要计算一下:2894135 ^ 3787313 mod 4028033

As you can see below i tried to use the BigInteger because i have really huge numbers.

正如你在下面看到的,我试图使用BigInteger,因为我有非常多的数字。

import java.lang.Math;
import java.util.Scanner;
public class BigInteger extends Number implements Comparable<BigInteger>
{
  public static void main(String[] args)
  {
  BigInteger result=new BigInteger(Math.pow(2894135,3787313) % 4028033);
    System.out.println(result);
  }
}

Error:

/tmp/java_Wcf144/BigInteger.java:19: error: BigInteger is not abstract and does not override abstract method doubleValue() in Number public class BigInteger extends Number implements Comparable ^ /tmp/java_Wcf144/BigInteger.java:24: error: constructor BigInteger in class BigInteger cannot be applied to given types;
BigInteger result=new BigInteger(Math.pow(2894135,3787313) % 4028033); ^ required: no arguments found: double reason: actual and formal argument lists differ in length 2 errors

/tmp/java_Wcf144/BigInteger.java:19:错误:BigInteger不是抽象的,并且不会覆盖抽象方法doubleValue()中的数字公共类BigInteger extends Number实现Comparable ^ /tmp/java_Wcf144/BigInteger.java:24:错误:构造函数类BigInteger中的BigInteger不能应用于给定类型; BigInteger结果= new BigInteger(Math.pow(2894135,3787313)%4028033); ^ required:找不到参数:double reason:实际和形式参数列表长度不同2错误

4 个解决方案

#1


5  

You will get wrong answer even after solving the errors because Math.pow(2894135,3787313), This will cause overflow in double and it will return largest possible value of double Double.MAX_VALUE.

即使在解决了错误之后你也会得到错误的答案,因为Math.pow(2894135,3787313),这将导致double的溢出,并且它将返回双倍Double.MAX_VALUE的最大可能值。

So you need to do all operations after converting them into BigInteger.

因此,您需要在将它们转换为BigInteger后执行所有操作。

import java.lang.Math;
import java.util.Scanner;
import java.math.BigInteger;
public class Main
{
  public static void main(String[] args)
  {
        BigInteger a=BigInteger.valueOf(2894135);
        BigInteger b=BigInteger.valueOf(3787313);
        BigInteger m=BigInteger.valueOf(4028033);
        BigInteger result=a.modPow(b,m); //calculates a^b %m
        System.out.println(result);
  }
}

EDIT: If you want to do this in more optimized way then you can use concept of Modular Exponentiation. This will give output in O(log(exponent)) complexity. Here you can't use bigger values because it may cause overflow in long which ends up in giving wrong result.

编辑:如果你想以更优化的方式做到这一点,那么你可以使用Modular Exponentiation的概念。这将给出O(log(指数))复杂度的输出。在这里你不能使用更大的值,因为它可能导致长时间溢出,最终导致错误的结果。

Code:

public class Main
{
  public static void main(String[] args)
  {
        long a=2894135;
        long b=3787313;
        long m=4028033;

        long result=modularExponentiation(a,b,m);
        System.out.println(result);
  }

    static long modularExponentiation(long a,long b,long m)
    {
        long result=1;
        while(b>0)
        {
            if(b % 2 ==1)
                result=(result * a)%m;
            a=(a*a)%m;
            b=b/2;
        }
        return result;
    }
}

#2


3  

You have not implemented doubleValue() method in your class. And you also need to rename your main class with some other name , Big Integer is a seperate object.

您尚未在类中实现doubleValue()方法。而且您还需要使用其他名称重命名主类,Big Integer是一个单独的对象。

#3


2  

Two issues: the first one about the mis-use of the BigInteger class.

两个问题:第一个关于错误使用BigInteger类的问题。

you declared your own BigInteger class, which sorry doesn't make much sense. If you want to be able to work with arbitrary sized Integer values; use the existing java.math.BigInteger class.

你声明了自己的BigInteger类,对不起来没有多大意义。如果您希望能够使用任意大小的整数值;使用现有的java.math.BigInteger类。

From there:

BigInteger result=new BigInteger(Math.pow(2894135,3787313) % 4028033);

You are not computing with BigInteger objects.

您不是使用BigInteger对象进行计算。

You are using int literals, to compute a value; and the result of that you intend to use as ctor argument for creating a single BigInteger.

您正在使用int literals来计算值;以及您打算用作创建单个BigInteger的ctor参数的结果。

You could go for:

你可以去:

BigInteger op1 = new BigInteger(2894135)
BigInteger op2 = new BigInteger(3787313);
BigInteger op3 = new BigInteger(4028033);
BigInteger result = op1.modpow(op2, op3); 

instead. Depending on the numbers you intend to use; you might or might not be able to do the "pow" calculation as you did before; using Math.pow() and work on double literals. But the above will work for any numbers that fitting into the JVM.

代替。取决于您打算使用的数字;您可能会或可能不会像以前那样进行“战俘”计算;使用Math.pow()并处理双重文字。但是上述内容适用于任何适合JVM的数字。

#4


1  

Why do you even want to create a BigInteger class.

为什么你甚至想创建一个BigInteger类。

BigInteger is already defined in Java, use it

BigInteger已经在Java中定义,使用它

https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html

Also, do all the operations on BigInteger, rather than converting them to BigInteger at the end.

此外,在BigInteger上执行所有操作,而不是在最后将它们转换为BigInteger。

Abhi

#1


5  

You will get wrong answer even after solving the errors because Math.pow(2894135,3787313), This will cause overflow in double and it will return largest possible value of double Double.MAX_VALUE.

即使在解决了错误之后你也会得到错误的答案,因为Math.pow(2894135,3787313),这将导致double的溢出,并且它将返回双倍Double.MAX_VALUE的最大可能值。

So you need to do all operations after converting them into BigInteger.

因此,您需要在将它们转换为BigInteger后执行所有操作。

import java.lang.Math;
import java.util.Scanner;
import java.math.BigInteger;
public class Main
{
  public static void main(String[] args)
  {
        BigInteger a=BigInteger.valueOf(2894135);
        BigInteger b=BigInteger.valueOf(3787313);
        BigInteger m=BigInteger.valueOf(4028033);
        BigInteger result=a.modPow(b,m); //calculates a^b %m
        System.out.println(result);
  }
}

EDIT: If you want to do this in more optimized way then you can use concept of Modular Exponentiation. This will give output in O(log(exponent)) complexity. Here you can't use bigger values because it may cause overflow in long which ends up in giving wrong result.

编辑:如果你想以更优化的方式做到这一点,那么你可以使用Modular Exponentiation的概念。这将给出O(log(指数))复杂度的输出。在这里你不能使用更大的值,因为它可能导致长时间溢出,最终导致错误的结果。

Code:

public class Main
{
  public static void main(String[] args)
  {
        long a=2894135;
        long b=3787313;
        long m=4028033;

        long result=modularExponentiation(a,b,m);
        System.out.println(result);
  }

    static long modularExponentiation(long a,long b,long m)
    {
        long result=1;
        while(b>0)
        {
            if(b % 2 ==1)
                result=(result * a)%m;
            a=(a*a)%m;
            b=b/2;
        }
        return result;
    }
}

#2


3  

You have not implemented doubleValue() method in your class. And you also need to rename your main class with some other name , Big Integer is a seperate object.

您尚未在类中实现doubleValue()方法。而且您还需要使用其他名称重命名主类,Big Integer是一个单独的对象。

#3


2  

Two issues: the first one about the mis-use of the BigInteger class.

两个问题:第一个关于错误使用BigInteger类的问题。

you declared your own BigInteger class, which sorry doesn't make much sense. If you want to be able to work with arbitrary sized Integer values; use the existing java.math.BigInteger class.

你声明了自己的BigInteger类,对不起来没有多大意义。如果您希望能够使用任意大小的整数值;使用现有的java.math.BigInteger类。

From there:

BigInteger result=new BigInteger(Math.pow(2894135,3787313) % 4028033);

You are not computing with BigInteger objects.

您不是使用BigInteger对象进行计算。

You are using int literals, to compute a value; and the result of that you intend to use as ctor argument for creating a single BigInteger.

您正在使用int literals来计算值;以及您打算用作创建单个BigInteger的ctor参数的结果。

You could go for:

你可以去:

BigInteger op1 = new BigInteger(2894135)
BigInteger op2 = new BigInteger(3787313);
BigInteger op3 = new BigInteger(4028033);
BigInteger result = op1.modpow(op2, op3); 

instead. Depending on the numbers you intend to use; you might or might not be able to do the "pow" calculation as you did before; using Math.pow() and work on double literals. But the above will work for any numbers that fitting into the JVM.

代替。取决于您打算使用的数字;您可能会或可能不会像以前那样进行“战俘”计算;使用Math.pow()并处理双重文字。但是上述内容适用于任何适合JVM的数字。

#4


1  

Why do you even want to create a BigInteger class.

为什么你甚至想创建一个BigInteger类。

BigInteger is already defined in Java, use it

BigInteger已经在Java中定义,使用它

https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html

Also, do all the operations on BigInteger, rather than converting them to BigInteger at the end.

此外,在BigInteger上执行所有操作,而不是在最后将它们转换为BigInteger。

Abhi