您的位置:首页 > 编程语言

BigInteger类方法代码简介

2020-02-01 01:20 736 查看

BigInteger类方法代码 详解 简介

懒得做PPT了,直接写代码。

import java.math.BigInteger;
import java.util.Random;

public class Biginteger {
public static void main(String[] args) {
/** @实现
* public class BigInteger extends Number implements Comparable<BigInteger>{
*     final int signum;
*     final int[] mag;
* }
*/

/** @范围测试
* >10^83886081   test.txt: 80.01MB    time: 578174ms
*
* BigInteger s = new BigInteger("10000000000");
* long t = System.currentTimeMillis();
* for(int i=0;i<1000000;i++){
* 		s = s.multiply(s);
* 		System.out.println(s.toString().length());
* 		System.out.println(System.currentTimeMillis()-t);
* }
*/

/** 常量
* @public static final BigInteger ZERO
* @public static final BigInteger ONE
* public static final BigInteger TWO (jdk9)
* public static final BigInteger TEN
*/
System.out.println("常数:" + BigInteger.ZERO + "," + BigInteger.ONE  + "," + BigInteger.TEN + '\n');

/** 构造
* @public BigInteger(String val)
* @public BigInteger(String val, int radix)
* public BigInteger(byte[] val) public BigInteger(int signum, byte[]magnitude)
* public BigInteger(int bitLength, int certainty, Random rnd)
* public BigInteger(int numBits, Random rnd) public BigInteger(String val)
*/
// 使用字符串构造,默认为十进制。使用字符串构造需要注意数值必须为正
System.out.println("字符串构造大数:" + new BigInteger("+123123123123123"));
System.out.println("int等类型构造大数:" + new BigInteger(String.valueOf(-1234567890)));
System.out.println("指定进制字符串构造大数:" + new BigInteger("ZZZZZZZ", 36));

byte[] by = { 15, 15 };
// 00001111 00001111 -> 3855
System.out.println("按位构造:" + new BigInteger(by));
System.out.println("按位构造(有符号):" + new BigInteger(-1, by));
// 生成素数的概率超过(1-0.5*certainty) : 1-0.5*0 = 1
System.out.println("生成随机素数:" + new BigInteger(50, 0, new Random()));
System.out.println("生成随机数:" + new BigInteger(50, new Random()) + '\n');

/** 方法-数学运算
* @BigInteger add(BigInteger val)   	return (this + val);
* @BigInteger subtract(BigInteger val) 	return (this - val);
* @BigInteger multiply(BigInteger val) 	return (this * val);
* @BigInteger divide(BigInteger val) 	return (this / val);
* @BigInteger mod(BigInteger m)			return (this % m); !取模 - m除数必须为正
* @BigInteger remainder(BigInteger val)	return (this % m); !求余
* @BigInteger pow(int exponent) 		return (this ^ m);
* @BigInteger abs() 					return ( |this| );
* @BigInteger gcd(BigInteger val)		return (this和val的最大公约数);
* BigInteger modInverse(BigInteger m) 	return (this^-1 % m); 乘法逆元 ax≡1(mod b)
* BigInteger[] divideAndRemainder(BigInteger val)  	return(this/val, this%val);
* BigInteger modPow(BigInteger exponent, BigInteger m) return(this^ex % m) 幂取模
*/
BigInteger big1 = new BigInteger("658209000");
BigInteger big2 = new BigInteger("158946600");
System.out.println("a+b  "+big1.add(big2));
System.out.println("a-b  "+big1.subtract(big2));
System.out.println("a*b  "+big1.multiply(big2));
System.out.println("a/b  "+big1.divide(big2));

System.out.println("a%b  "+big1.mod(big2));
System.out.println("a%b  "+big1.remainder(big2.negate()));
System.out.println("b^2  "+big2.pow(2));
System.out.println("|a|  "+big1.abs());
//jdk1.9开始添加了sqrt方法
System.out.println("gcd(a,b)  "+big1.gcd(big2));
System.out.println("a^-1%b    "  +new BigInteger("7").modInverse(new BigInteger("20")));
System.out.println("a/b       "+big1.divideAndRemainder(big2)[0] +"……"+ big1.divideAndRemainder(big2)[1]);
System.out.println("a^b%c     "+big1.modPow(big1, big2));
/**@modPow与快速幂取模算法效率测试 n=1e7
* @modPow  660ms   ->基于位操作
* @Fastpow 3300ms
*
long t = System.currentTimeMillis();
for(int i=0;i<10000000;i++){
big1 = big1.modPow(new BigInteger(String.valueOf(i)), big2);
}
System.out.println(System.currentTimeMillis()-t);

t = System.currentTimeMillis();
for(int i=0;i<10000000;i++) {
long a = 658209000, b = i, p = 158946600 ,sum = 1;
while(b!=0) {
if((b&1)==1) {
sum = (sum*a)%p;b--;
}
b/=2;a = a*a %p;
}
}
System.out.println(System.currentTimeMillis()-t);
*/

/** 方法-基本操作
* @int signum()							return sign;
* @BigInteger negate() 					return -this;
* @boolean equals(Object x)  			return this==val;
* @BigInteger max(BigInteger val) 		return max(this, val);
* @BigInteger min(BigInteger val)		return min(this, val);
* @int compareTo(BigInteger val)		return this==val?0:(this>val?1:-1);
*/
big1 = new BigInteger(String.valueOf(-1000));
big2 = new BigInteger("1000");
System.out.println("符号位	"+big1.signum());
System.out.println("负值		"+big1.negate());
System.out.println("是否相等	"+big1.equals(big2));
System.out.println("比较大小	"+big1.compareTo(big2.negate()));
System.out.println("两数最大者	"+big1.max(big2));
System.out.println("两数最小者	"+big1.min(big2));

/** 方法-类型转换
* int intValue()
* long longValue()
* float floatValue()
* double doubleValue()
*
* ArithmeticException: BigInteger out of ... range
*
* byte byteValueExact()
* short shortValueExact()
* int intValueExact()
* long longValueExact()
*
* @String toString()
* @String toString(int radix)
* byte[] toByteArray()
* static BigInteger valueOf(long val)
*/
big1 = new BigInteger(String.valueOf("-9999999999999999"));
big2 = new BigInteger("10000000000000");
System.out.println("byte   "+big1.byteValue());
System.out.println("int    "+big1.intValue());
System.out.println("long   "+big1.longValue());
System.out.println("float  "+big1.floatValue());
System.out.println("string "+big1.toString());
System.out.println("string "+big1.toString(16)+'\n');
/**@非大数进制转换效率测试 n=1e7 十进制转十六进制
* @BigInteger 1800ms
* @Ordinary   120ms
*
long t = System.currentTimeMillis();
for(int i=0;i<10000000;i++){ //String.valueOf(i)
String a = new BigInteger("999999999").toString(16);
}
System.out.println(System.currentTimeMillis()-t);

//除法取余(最快)
t = System.currentTimeMillis();
for(int i=0;i<10000000;i++) {
int[] a = new int[10];
int len = 0, s=999999999;
while(s!=0) {
a[len++]=s%16;
s/=16;
}
}
System.out.println(System.currentTimeMillis()-t);

//二进制转十六进制(最慢)
t = System.currentTimeMillis();
for(int i=0;i<100000;i++) {
int[] num = new int[10000];
int[] o = {1,2,4,8};
int len = 0, s=999999999;
while(s!=0) {
for(int j=0;j<4;j++){
num[len]+=(s&1)*o[j];
s>>=1;
}
len++;
}
//for(int j=len-1;j>=0;j--)//输出十六进制
//	System.out.print(Integer.toHexString(num[j]));
//System.out.println();
}
System.out.println(System.currentTimeMillis()-t);
*/

/** @方法-位运算
* BigInteger and(BigInteger val) 		return (this & val);
* BigInteger or(BigInteger val) 		return (this | val);
* BigInteger not()  					return (~this);
* BigInteger andNot(BigInteger val)  	return (this & ~val);
* BigInteger xor(BigInteger val) 		return (this ^ val);
* BigInteger shiftRight(int n)  		return (this >> val);
* BigInteger shiftLeft(int n)  		return (this << val);
*/
System.out.println("a&b   "+big1.and(big2));
System.out.println("a|b   "+big1.and(big2));
System.out.println("~a    "+big1.not());
System.out.println("a&~b  "+big1.andNot(big2));
System.out.println("a^b   "+big1.xor(big2));
System.out.println("a<<3  "+big1.shiftLeft(3));
System.out.println("a>>-2 "+big1.shiftRight(-2)+'\n');

/**
* int bitCount()   返回该BigInteger的二进制补码表示中与其符号位不同的位数。
* int bitLength()  返回此BigInteger的最小二进制补码表示中的位数, 不包括符号位。
* BigInteger clearBit(int n)  return this&~(1<<n);    将指定的位清零
* int getLowestSetBit()       return this==0?-1:log2(this&-this);  返回此BigInteger中最右(最低位)一位的索引(最右边一位右侧的零位数)。
* BigInteger flipBit(int n)   return this^(1<<n);     返回一个BigInteger,其值等于此BigInteger,指定的位被翻转。
* BigInteger setBit(int n)    return this|(1<<n);     返回一个BigInteger,其值等于具有指定位集合的BigInteger。
* boolean testBit(int n)      return (this&(1<<n))!=0;返回 true当且仅当指定的位被设置。
*
* int hashCode()  		返回此BigInteger的哈希码。
* boolean isProbablePrime(int certainty)   返回true如果这个BigInteger可能是素数, false如果它是绝对复数。
* BigInteger nextProbablePrime()           返回大于此BigInteger的第一个整数,这可能是素数。
* static BigInteger probablePrime(int bitLength, Random rnd)   返回一个正的BigInteger,它可能是素数,具有指定的位长度。
*/
}

/**
* @亿进制(我的博客)
* https://blog.csdn.net/qq_43312718/article/details/100573601
* @mod和remainder区别
* https://blog.csdn.net/qq_34115899/article/details/79683041
* @乘法逆元
* https://blog.csdn.net/sdfzchy/article/details/76098066
*/

}
  • 点赞 2
  • 收藏
  • 分享
  • 文章举报
猪猪侠的Laser_Cannon 发布了8 篇原创文章 · 获赞 8 · 访问量 903 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: