2007/11/22

【Java】 BigInteger 追記01

【大整数】
*多数桁処理のために「大整数(BigInteger)」のクラスを使う.

使用パッケージ宣言:
import java.math.BigInteger;


定義(BigInteger):
「大整数」はオブジェクトとして定義する必要があるので以下のように定義する.


BigInteger b = new BigInteger(String);


Stringには文字列を入れる.
*また,BigIntegerの配列は他の配列の宣言と同じである.

BigInteger [] b = new BigInteger[30];

*これでBigInteger型の配列,b[30]を宣言したことになる.


演算(BigInteger):
オブジェクトとして定義した「大整数」はオブジェクトの演算メソッドを用いて演算を行う.主な演算メソッドを次にあげる.

返り値型種類記述
BigInteger加法:a + ba.add(b);
BigInteger減法:a - ba.subtract(b);
BigInteger乗法:a * ba.multiply(b);
BigInteger除法:a / ba.divide(b);
BigInteger剰余:a % ba.remainder(b);
BigIntegerべき乗:a ^ ba.pow(int b);
BigInteger絶対値:aa.abs();
BigInteger符号反転:a = - aa.negate();
BigInteger最小値:min aa.min();
BigInteger最大値:max aa.max();
BigInteger合同:a ≡ c (mod b)c = a.mod(b);
BigIntegerべき乗の合同:a ^ m ≡ c (mod b)c = a.modPow(m, b);
BigInteger逆元(mod b):a ^ -1 (mod b)a.modInverse(b);
BigInteger最大公約数(gcd(a, b)):gcd(a, b)a.gcd(b);
boolean等号:a == ba.equals(b);
int大小比較:a <>a.compareTo(b);


*表中のa, b, cは特に断りがない限り,BigInteger型.
*「equals」はboolean型のメソッド.オブジェクトの同一性をチェック
*「compareTo」はint型のメソッドで,「a < a =" b」の場合0,「a"> b」の場合1となる.


出力(BigInteger):
*大整数はオブジェクト型のデータなので,出力の際はデータ変換で文字列型にしてから出力する.
System.out.println("BigInteger b = "+b.toString());

0 件のコメント: