2017-04-25 20 views
-1

我需要計算這樣的:2894135^3787313 MOD 4028033我簡單的計算與Bigintegers不工作

正如你可以看到下面我試圖使用的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); 
    } 
} 

錯誤:

/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

+4

類重命名爲'BigInteger' – Reimeus

+3

除了別的其它別的東西,你想要做的所有算術*第一個*,然後轉換*結果*到'BigInteger'。這是行不通的。你應該在'BigInteger'中做所有算術*。是的,正如Reimeus所說,你真的不想將自己的類聲明爲BigInteger ... –

+0

但*不要*將它重命名爲Biginteger(如在標題中)。 –

回答

4

即使解決了錯誤,因爲Math.pow(2894135,3787313),這將導致doubleoverflow,它將返回的雙Double.MAX_VALUE最大可能值後,您會得到錯誤的答案。

因此,您需要在將它們轉換爲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); 
    } 
} 

編輯: 如果你想這樣做更優化的方式,那麼你可以使用Modular Exponentiation概念。這會給O(log(exponent))輸出帶來複雜性。在這裏你不能使用更大的值,因爲它可能會導致overflowlong這最終導致錯誤的結果。

代碼:

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; 
    } 
} 
+0

這個!非常感謝你的時間,我得到了正確的結果:) – Thomas

+0

雖然你稍後來了,你在哪裏第一個給出完全正確的答案。我爲此而贊成! – GhostCat

+0

AFAIK,模塊冪運算也是BigInteger.modPow使用的。但是對於BigIntegers來說,當然不是很長時間。 –

3

你有沒有在你的類實現doubleValue()方法。 而且您還需要使用其他名稱重命名主類,Big Integer是一個單獨的對象。

2

兩個問題:第一個關於BigInteger類的錯誤使用。

你宣佈你的擁有 BigInteger類,對不起沒有太大意義。如果您希望能夠使用任意大小的Integer值;使用現有的java.math.BigInteger類。

從那裏:

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

您是計算的BigInteger對象。

您正在使用int文字來計算值;以及您打算將其用作ctor參數來創建單個BigInteger的結果。

你可以去:

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

代替。取決於您打算使用的數字;你可能會也可能不會像以前那樣做「pow」計算;使用Math.pow()和double文字。但是,上述內容適用於任何適合JVM的號碼。

+0

也是構造函數'BigInter(long/double)'不起作用。使用'BigInteger.valueOf((long)Math.pow(2894135,3787313))' – XtremeBaumer

+0

這是事實,但這不是錯誤所在。 – Ivar

+0

謝謝你的幫助!下面Sanket Makani的代碼解決了我所有的問題,並做了這麼大的計算! – Thomas