BigInteger用法記錄[Java]

1. 靜態(tài)方法

probablePrime(int bitLength, Random rnd)

  • 隨機(jī)得到一個(gè)bit長(zhǎng)度為bitLength的素?cái)?shù)

     BigInteger bigInteger = BigInteger.probablePrime(3, new Random());
     System.out.println(bigInteger.toString());//10進(jìn)制輸出 , 結(jié)果: 5 或者 7
    

valueOf(long val)

  • 得到一個(gè)值為val的BIgInteger

     BigInteger bigInteger = BigInteger.valueOf(100);
     System.out.println(bigInteger.toString(10));//10進(jìn)制輸出 , 結(jié)果 : 100
    

2. 構(gòu)造方法

BigInteger(byte[] val)

  • BigInteger的大小為val的順序拼接結(jié)果

     byte[] val = new byte[]{0x11,0x22,0x33};
     BigInteger bigInteger = new BigInteger(val);
     System.out.println(bigInteger.toString(16));//16進(jìn)制輸出 , 結(jié)果 : 112233
    

BigInteger(int signum, byte[] magnitude)

  • 得到大小為 magnitude , 正負(fù)值為 signum 的 BigInteger

     byte[] magnitude= new byte[]{0x11,0x22,0x33};
     BigInteger bigInteger = new BigInteger(1,magnitude);
     System.out.println(bigInteger.toString(16));//輸出結(jié)果 : 112233
     
     bigInteger = new BigInteger(-1,magnitude);
     System.out.println(bigInteger.toString(16));//輸出結(jié)果 : -112233magnitude = new byte[0];
     
     bigInteger = new BigInteger(0,magnitude);
     System.out.println(bigInteger.toString(16));//輸出結(jié)果 : 0 , (signum 值為 0 時(shí) , magnitude 必須為 byte[0] , 否則拋出異常)
    

BigInteger(String val, int radix)

  • 得到大小為val , 以 radix 為基數(shù)的 BigInteger

     BigInteger bigInteger = new BigInteger("1F", 16);
     System.out.println(bigInteger.toString(16));//輸出結(jié)果 1f
     System.out.println(bigInteger.toString(10));//輸出結(jié)果 31
    

3 . 成員方法

nextProbablePrime()

  • 得到比當(dāng)前數(shù)字大的第一個(gè)素?cái)?shù)

     BigInteger bigInteger = BigInteger.valueOf(9);
     System.out.println(bigInteger.nextProbablePrime().toString());//結(jié)果 : 11
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語(yǔ)法,類相關(guān)的語(yǔ)法,內(nèi)部類的語(yǔ)法,繼承相關(guān)的語(yǔ)法,異常的語(yǔ)法,線程的語(yǔ)...
    子非魚_t_閱讀 34,823評(píng)論 18 399
  • 【程序1】 題目:古典問(wèn)題:有一對(duì)兔子,從出生后第3個(gè)月起每個(gè)月都生一對(duì)兔子,小兔子長(zhǎng)到第三個(gè)月后每個(gè)月又生一對(duì)兔...
    葉總韓閱讀 5,229評(píng)論 0 41
  • Java經(jīng)典問(wèn)題算法大全 /*【程序1】 題目:古典問(wèn)題:有一對(duì)兔子,從出生后第3個(gè)月起每個(gè)月都生一對(duì)兔子,小兔子...
    趙宇_阿特奇閱讀 2,084評(píng)論 0 2
  • 一、 1、請(qǐng)用Java寫一個(gè)冒泡排序方法 【參考答案】 public static void Bubble(int...
    獨(dú)云閱讀 1,504評(píng)論 0 6
  • 魔術(shù)貼廣泛應(yīng)用于日常生活中各個(gè)方面,如木材家具,五金器材,鞋包箱等,但我們要注意使用的時(shí)候不能用力,小心維護(hù),延長(zhǎng)...
    楊小刀Team閱讀 5,536評(píng)論 0 0

友情鏈接更多精彩內(nèi)容