《java练习_大数运算_BigInteger.pdf》由会员分享,可在线阅读,更多相关《java练习_大数运算_BigInteger.pdf(4页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、/*大数运算*BigInteger*求91 的 92 次方*求它除以 100 的余数*大数运算 Biginteger类的方法调用*/package com.test1;import java.math.*;import java.math.BigInteger;publicclass test100 /*param args*/static BigInteger k=BigInteger.valueOf(1);static BigInteger j=BigInteger.valueOf(91);static BigInteger n;BigDecimal l=new BigDecimal(10
2、0);static BigInteger m=new BigInteger(100);publicstaticvoid main(String args)/TODO Auto-generated method stub/k=BigInteger.valueOf(1);/k=new BigInteger(1);for(int i=1;i=92;i+)k=k.multiply(j);System.out.println(k.toString();/n=k.remainder(m);n=k.remainder(m);System.out.println(n.toString();/java大数运算
3、Biginteger类的方法调用/以下代码与本练习没有必要关系,可以删除class BigIntegerGet public String getAdd(String Str1,String Str2)String Str3=new String();BigInteger BigInt1=new BigInteger(Str1);BigInteger BigInt2=new BigInteger(Str2);BigInt1=BigInt1.add(BigInt2);/加 Str3=BigInt1.toString();return Str3;public String getSubtract(
4、String Str1,String Str2)String Str3=new String();BigInteger BigInt1=new BigInteger(Str1);BigInteger BigInt2=new BigInteger(Str2);BigInt1=BigInt1.subtract(BigInt2);/减 Str3=BigInt1.toString();return Str3;public String getMultiply(String Str1,String Str2)String Str3=new String();BigInteger BigInt1=new
5、BigInteger(Str1);BigInteger BigInt2=new BigInteger(Str2);BigInt1=BigInt1.multiply(BigInt2);/乘 Str3=BigInt1.toString();return Str3;public String getDivide(String Str1,String Str2)String Str3=new String();BigInteger BigInt1=new BigInteger(Str1);BigInteger BigInt2=new BigInteger(Str2);BigInt1=BigInt1.d
6、ivide(BigInt2);/除 Str3=BigInt1.toString();return Str3;public String getRemainder(String Str1,String Str2)/String Str3=new String();BigInteger BigInt1=new BigInteger(Str1);BigInteger BigInt2=new BigInteger(Str2);BigInt1=BigInt1.remainder(BigInt2);/余数 Str3=BigInt1.toString();return Str3;public String
7、getGcd(String Str1,String Str2)String Str3=new String();BigInteger BigInt1=new BigInteger(Str1);BigInteger BigInt2=new BigInteger(Str2);BigInt1=BigInt1.gcd(BigInt2);/最大公约数 Str3=BigInt1.toString();return Str3;public String getPow(String Str1,String Str2)String Str3=new String();BigInteger BigInt1=new
8、 BigInteger(Str1);int Int2=Integer.valueOf(Str2);BigInt1=BigInt1.pow(Int2);/方 Str3=BigInt1.toString();return Str3;public String getMod(String Str1,String Str2)String Str3=new String();BigInteger BigInt1=new BigInteger(Str1);BigInteger BigInt2=new BigInteger(Str2);BigInt1=BigInt1.mod(BigInt2);/模 Str3
9、=BigInt1.toString();return Str3;public String getModInverse(String Str1,String Str2)String Str3=new String();BigInteger BigInt1=new BigInteger(Str1);BigInteger BigInt2=new BigInteger(Str2);BigInt1=BigInt1.modInverse(BigInt2);/倒数取模 Str3=BigInt1.toString();return Str3;public String getMax(String Str1,
10、String Str2)String Str3=new String();BigInteger BigInt1=new BigInteger(Str1);BigInteger BigInt2=new BigInteger(Str2);BigInt1=BigInt1.max(BigInt2);/返回较大一个数的值 Str3=BigInt1.toString();return Str3;public String getMin(String Str1,String Str2)String Str3=new String();BigInteger BigInt1=new BigInteger(Str
11、1);BigInteger BigInt2=new BigInteger(Str2);BigInt1=BigInt1.min(BigInt2);/返回较小一个数的值 Str3=BigInt1.toString();return Str3;publicint getHashcode(String Str)int hash=-1;BigInteger BigInt=new BigInteger(Str);hash=BigInt.hashCode();return hash;publicboolean getIsProbablePrime(String Str,int certainty)boolean flag=false;BigInteger BigInt=new BigInteger(Str);flag=BigInt.isProbablePrime(certainty);/素数return flag;